How to clear data in a DataGridView in C#?
In C#, you can use the following code to clear the data in a DataGridView:
- Clear method is used to empty all data rows.
dataGridView1.Rows.Clear();
- Rebind the DataGridView with a new empty DataTable.
DataTable dt = new DataTable();
dataGridView1.DataSource = dt;
- Remove all columns from the DataGridView and then re-add empty columns.
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
// 添加空列
dataGridView1.Columns.Add("Column1", "列1");
dataGridView1.Columns.Add("Column2", "列2");
// ...
The above methods can be chosen based on the actual situation to clear the data in the DataGridView.