gridview dataBind不起作用

时间:2012-10-15 16:44:33

标签: asp.net gridview

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

 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>

1 个答案:

答案 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();