2016-11-15 91 views
0

當數據綁定到gridview時我沒有得到任何東西。誰能幫忙?沒有從數據庫中獲取數據到gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="1000px" AllowPaging="True" PageSize="8" `CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"> 
    <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle> 
    <PagerStyle CssClass="pgr"></PagerStyle> 
</asp:GridView> 

C#

SqlCommand cmd1 = new SqlCommand("SELECT cars.carid, cars.make, cars.model, cars.condition, cars.amount, img.img FROM cars INNER JOIN img ON cars.carid = img.imgid where model = '" + mod + "' and make = '" + mak + "' and variant = '" + var + "' and color = '" + col + "' and condition = '" + cond + "'",con); 
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1); 
DataTable dt = new DataTable(); 

sda1.Fill(dt); 
GridView1.DataSource = dt; 
GridView1.DataBind(); 
+1

是否有錯誤?你看到dt中的數據了嗎? – Sajeetharan

+0

沒有錯誤..空白頁面顯示 – Samsam

回答

0

設置AutoGenerateColumns="True"否則,你需要定義列和綁定。

閱讀更多MSDN文檔的詳細信息DataGridView.AutoGenerateColumns Property

+0

仍然沒有顯示任何東西 – Samsam

+0

更改您的SQL爲「SELECT cars.carid,cars.make,cars.model,cars.condition,cars.amount,img.img FROM cars INNER JOIN img ON cars.carid = img.imgid「運行並檢查輸出 – Damith

+0

哦,是的,現在得到輸出,.. – Samsam