简体   繁体   English

asp.net gridview不显示数据

[英]asp.net gridview doesn't display data

protected void Button1_Click1(object sender, EventArgs e)
{
    SqlConnectionStringBuilder connb = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["SCS"].ConnectionString);      
    using (SqlConnection conn = new SqlConnection(connb.ConnectionString))
    {
        SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
        DataTable tb = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        da.Fill(tb);
        tb.AcceptChanges();
        GridView1.DataSource = tb;
        GridView1.DataBind();
     }
}

This is my code in C# asp.net application. 这是我在C#asp.net应用程序中的代码。 I want to display SQL table in gridview. 我想在gridview中显示SQL表。

<Columns>
  <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="name" />
  <asp:BoundField ItemStyle-Width="150px" DataField="lastname" HeaderText="lastname" />
  <asp:BoundField ItemStyle-Width="150px" DataField="ID" HeaderText="ID" />
</Columns>

IT shows empty gridview(When I press button1). 它显示空的gridview(当我按下button1时)。 It doesn't shows any error message. 它不显示任何错误消息。 Connection string works, SQL command affects rows, But it still doesn't show any data on gridview!!! 连接字符串工作,SQL命令影响行,但它仍然没有显示gridview上的任何数据!

Can anyone help me? 谁能帮我?

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);

GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Seems like your query never gets executed 好像你的查询永远不会被执行

cmd.ExecuteNonQuery () 

This should have happened before this line 这应该发生在这一行之前

SqlDataAdapter da = new SqlDataAdapter(cmd); 

You could be getting exception in DataBind() - use a try-catch block. 您可能在DataBind()获得异常 - 使用try-catch块。 This could happen due to a missing column in the DataTable that is being used in a bound field. 这可能是由于在绑定字段中使用的DataTable中缺少列。

 protected void Button1_Click1(object sender, EventArgs e)
 {

   SqlConnectionStringBuilder connb = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["SCS"].ConnectionString);
   using (SqlConnection conn = new SqlConnection(connb.ConnectionString))
   {
    try
    {
      SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
      DataTable tb = new DataTable();

      SqlDataAdapter da = new SqlDataAdapter(cmd);

      da.Fill(tb);
      tb.AcceptChanges();
      GridView1.DataSource = tb;
      GridView1.DataBind();
      GridView1.Rows[0].Cells[0].Text. = "a";
    }
    catch(Exception ex)
    {
         Response.Write(ex.Message);
    }     
   }           
}
  • You should set AutoGenerateColumns="false" , if you want to bind specify column.Please check it 如果要绑定指定列,则应设置AutoGenerateColumns="false" 。请检查它

  • And also need to check have you set visible="false" or style="display:none" in your gridview 还需要检查是否在gridview中设置了visible="false"style="display:none"

  • And check DataField must matched the table column name 并检查DataField必须与表column名匹配

I had a problem where I was getting data in the datasource and the GridView wasn't showing up at all. 我有一个问题,我在数据源中获取数据,而GridView根本没有出现。 I had AutoGenerateColumns = "true" in the Gridview definition on the page. 我在页面的Gridview定义中有AutoGenerateColumns =“true”。 For some reason, when I set up a breakpoint and looked at the properties of the Gridview in the code behind, it was still set to false. 出于某种原因,当我设置断点并在后面的代码中查看Gridview的属性时,它仍然设置为false。 I set it to true in the code behind before setting the Gridview equal to the datasource and doing the databind. 在将Gridview设置为等于数据源并执行数据绑定之前,我在后面的代码中将其设置为true。 That worked. 那很有效。

       SqlCommand cmd = new SqlCommand("Select * from dbo.Users;", conn);
       SqlDataAdapter da = new SqlDataAdapter(cmd);      
       ds = new DataSet();
       da.Fill(ds, "usersdata");
       GridView1.DataSource = ds.Tables["usersdata"];
       GridView1.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM