2014-03-04 71 views
0

當我嘗試從Sql服務器數據庫中獲取數據並將其顯示在GridView中時,我正在獲得以下內容。我的代碼如下:我們如何在asp.net中的網格視圖中顯示數據?

string connstr = @"Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123"; 
     DataSet ds = new DataSet(); 
     SqlDataAdapter da; 
     DataTable dt = new DataTable(); 

     SqlConnection conn = new SqlConnection(connstr); 
     conn.Open(); 
     da = null; 
     string strsql = "Select File_ID,File_Name,File_Type from Upload_file"; 
     da = new SqlDataAdapter(strsql, conn); 
     da.Fill(ds, "Upload_file"); 

     GridView1.DataSource = ds.Tables["Upload_file"].DefaultView; 
     GridView1.DataBind(); 

enter image description here

+0

這不是真的清楚你想要輸出。不要讓我們猜測。 –

回答

0

試試這個

  string connstr = @"Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123"; 
      SqlConnection conn = new SqlConnection(connstr); 
      conn.Open(); 
      SqlDataAdapter adapter = new SqlDataAdapter("Select File_ID,File_Name,File_Type from Upload_file ", conn); 
      DataTable dt = new DataTable(); 
      adapter.Fill(dt); 
      conn.Close(); 
      GridView1.DataSource = dt; 

      GridView1.DataBind(); 

希望這將有助於

1

要在您的視圖顯示網格視圖的數據做這樣的事情(ASP.Net MVC剃刀):

<table> 
<tr> 
<td> 
Heading 
</td> 
</tr> 

@foreach(var item in Model) 
{ 
<tr> 
<td> 
@* Something that you want to display *@ 
</td> 
</tr> 
} 

</table> 
0

最簡單的方法可能是:

//Create sql DATA source by passing connection string and select command 
SqlDataSource mySqlDataSource= new SqlDataSource("Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123", "Select File_ID,File_Name,File_Type from Upload_file"); 

//Assign the data source to GridView data source 
GridView1.DataSource = mySqlDataSource; 
//Do data binding 
GridView1.DataBind(); 
+0

@ user3377740:對此有何更新? – user3240361

相關問題