2012-10-15 39 views
1

这是我的代码来绑定gridview。但是当我运行时,我什么都看不到(没有gridview只是空的),但是当我使用sqldatsource时,我可以绑定所有的。 我该如何解决这个问题?gridview dataBind不工作

SqlConnection conn = new SqlConnection(yol); 

     conn.Open(); 

    SqlCommand komut = new SqlCommand("select * from duyuru", conn); 
    SqlDataReader dr = komut.ExecuteReader(); 
    DataTable dt = new DataTable(); 
    dt.Load(dr); 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    conn.Close(); 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onrowdeleting="GridView1_RowDeleting" AllowPaging="True" 
        BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" CellSpacing="2" onrowcommand="GridView1_RowCommand"> 
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
       </asp:GridView> 
+0

你可以添加你的GridView的代码呢? – dgarbacz

+0

已添加html代码 – Acablack

+0

您确定您查询的表格中有数据吗? –

回答

0

试试这个:

SqlConnection conn = new SqlConnection(yol); 

     conn.Open(); 

    SqlCommand komut = new SqlCommand("select * from duyuru", conn); 

SqlDataAdapter adapter = new SqlDataAdapter(komut); 
      DataSet ds = new DataSet(); 

      adapter.Fill(ds, "Products");//here enter your table name 

      GridView1.DataSource = ds; 
      GridView1.DataBind(); 
+0

没有不工作空屏幕,没有什么 – Acablack

+0

我试过这么多次,但它不工作,我不知道为什么?所有关于vs2008? – Acablack