【问题标题】:how to bind data to gridview in windowsApplication C#如何在windowsApplication C#中将数据绑定到gridview
【发布时间】:2014-12-02 05:39:55
【问题描述】:

我在按钮中有以下代码。通过单击该按钮,我从 sql server 检索数据并显示在 GridView 中。我可以检索并填写 dt。但我的数据未显示在网格视图中, 问题是什么?请把我打爆。

       private void gvIPretrieve_Click(object sender, EventArgs e)
        {
        connstring();
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("gvretrieve_ip", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure ;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        dataGridView2.DataSource = dt;
                    }
                }
            }
          }

【问题讨论】:

  • 执行命令后'dt'中是否有数据?
  • 尝试添加dataGridView2.DataBind();在 dataGridView2.DataSource = dt; 之后

标签: c# gridview


【解决方案1】:

确保 Windows 应用程序中没有 Gridview。有一个控件以行和列的形式显示数据,即 DataGridView。这是最有用的Link,用于通过数据表从 sql server 数据库绑定数据

【讨论】:

    【解决方案2】:

    尝试使用 dataGridView2.DataSource = dt.DefaultView;

    【讨论】:

      【解决方案3】:

      先生,asp.net 还是 windows?需要在dataGridView2.DataSource=dt;后面加上dataGridView2.DataBind();

      希望,它会有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多