C# 如何清除 DataGridView 以便新数据可以填充单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10129367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:33:32  来源:igfitidea点击:

How to clear DataGridView so that new data can fill the cells?

c#datagridview

提问by Alternatex

I'm working on a little project of mine in C# and SQL Server and I can't figure out a way to clear out the cells in a DataGridView so that new data can fill the cells. I am loading the data with arguments such as a value in a column. For example, I want to load all the books for a certain Author, and then I want to load all the books for another author. Since the DataGridView isn't cleared, the books of the second author are just loaded below the rows of the previous data and it continues like this.

我正在用 C# 和 SQL Server 处理我的一个小项目,但我想不出一种方法来清除 DataGridView 中的单元格,以便新数据可以填充单元格。我正在加载带有参数(例如列中的值)的数据。例如,我想加载某个作者的所有书籍,然后我想加载另一个作者的所有书籍。由于未清除DataGridView,因此第二作者的书籍只是加载在前一个数据行的下方,并继续这样。

I want to be able to clear the DataGridView before the new data is loaded so it can be the only data I see when I click my so-called "Load data" button.

我希望能够在加载新数据之前清除 DataGridView,因此当我单击所谓的“加载数据”按钮时,它可以是我看到的唯一数据。

At the moment, this is what my code looks like more or less:

目前,这就是我的代码或多或少的样子:

    SqlConnection connect;
    DataSet ds1 = new DataSet();
    SqlDataAdapter da;

    connect = new SqlConnection();
    connect.ConnectionString = "Data Source=THEPC-PC\SQLExpress;Initial Catalog=TheDatabase;Integrated Security=True";
    string sql = "Select * from table WHERE author = 'BookAuthor'";
    da = new SqlDataAdapter(sql, connect);
    da.Fill(ds1, "table");
    table_dataGridView.AutoGenerateColumns = false;
    table_dataGridView.DataSource = ds1.Tables["table"];
    connect.Open();
    connect.Close();

A simplified version on how the DataGridView looks if I clicked "Load data" for author1 and then for author2 and then for author1 again:

如果我为 author1 然后为 author2 然后为 author1 再次单击“加载数据”,则 DataGridView 外观的简化版本:

------------------------
| author1 |  bookONE   |
------------------------
| author1 |  bookTWO   |
------------------------
| author2 |   bookA    |
------------------------
| author1 |  bookONE   |
------------------------
| author1 |  bookTWO   |
------------------------

Is there a piece of code I can add before the query that will erase all previous data found in the DataGridView?

我可以在查询之前添加一段代码来删除在 DataGridView 中找到的所有以前的数据吗?

回答by ABH

Have you tried table_dataGridView.DataSource = nulland table_dataGridView.Rows.Clear()before adding new rows?

您是否尝试过table_dataGridView.DataSource = nulltable_dataGridView.Rows.Clear()添加新行过吗?

As your table_dataGridViewis bound to a DataSource, then proper code for removing existing rows should be;

由于您table_dataGridView绑定到数据源,因此删除现有行的正确代码应该是;

if (table_dataGridView.DataSource != null)
{
     table_dataGridView.DataSource = null;
}
else
{
    table_dataGridView.Rows.Clear();
}

回答by shanish

Why dont you try setting the DataSource as null like table_dataGridView.DataSource=null

为什么不尝试将 DataSource 设置为 null,如 table_dataGridView.DataSource=null

回答by Gabriel GM

The code you provided should erase the previous data from the DataGridViewand replace it with the new data.
The problem might be that you append the rows to your DataTable. Can you check if ds1.Tables["table"]contains only the data you need?

您提供的代码应该从 中删除以前的数据DataGridView并将其替换为新数据。
问题可能是您将行附加到您的DataTable. 你能检查一下是否ds1.Tables["table"]只包含你需要的数据吗?

回答by Thilina Anuradh

This worked for me

这对我有用

        cl.OpenCon();
        String data = "select ID,Name,Price from Items";
        SqlCommand ncmd = new SqlCommand(data);
        ncmd.Connection = cl.ReturnCon();
        SqlDataReader rs = ncmd.ExecuteReader();
        dt = new DataTable();
        dt.Load(rs);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        cl.CloseCon();

protected void Clear_Click1(object sender, EventArgs e) { dt.Clear(); GridView1.DataSource = dt; GridView1.DataBind(); }

protected void Clear_Click1(object sender, EventArgs e) { dt.Clear(); GridView1.DataSource = dt; GridView1.DataBind(); }

回答by user3822219

on event click add

在事件点击添加

BindingSource.AddNew();

BindingSource.AddNew();