1

这是我绑定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>
4

1 回答 1

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();
于 2012-10-15T16:52:43.377 回答