C# 如何清除数据网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13137591/
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-10 01:42:54  来源:igfitidea点击:

How to clear a data grid view

c#datagrid

提问by Wizard

I am trying to populate a DataGridView based on the selected item in a ComboBox, I have this part working.

我正在尝试根据 ComboBox 中的选定项目填充 DataGridView,这部分工作正常。

However, I need to be able to clear the grid before adding the new data from a new item rather than it just adding on to the end.

但是,我需要能够在从新项目添加新数据之前清除网格,而不仅仅是添加到最后。

How do I clear a DataGridView before adding items to it?

如何在向 DataGridView 添加项目之前清除它?

采纳答案by Trevor Pilley

Firstly, null the data source:

首先,将数据源设为 null:

this.dataGridView.DataSource = null;

Then clear the rows:

然后清除行:

this.dataGridView.Rows.Clear();

Then set the data source to the new list:

然后将数据源设置为新列表:

this.dataGridView.DataSource = this.GetNewValues();

回答by Pankaj Agarwal

DataGrid.DataSource = null;
DataGrid.DataBind();

回答by Priyank Patel

You can clear DataGridViewin this manner

您可以DataGridView通过这种方式 清除

dataGridView1.Rows.Clear();
dataGridView1.Refresh();

If it is databoundthen try this

如果是,databound那么试试这个

dataGridView1.Rows.Clear() // If dgv is bound to datatable
dataGridView1.DataBind();

回答by Asif

dataGridView1.Rows.Clear();
dataGridView1.Refresh();

回答by Rohit Vyas

You can assign the datasource as null of your data grid and then rebind it.

您可以将数据源分配为数据网格的空值,然后重新绑定它。

dg.DataSource = null;
dg.DataBind();

回答by vignesh

refresh the datagridview and refresh the datatable

刷新datagridview并刷新datatable

dataGridView1.Refresh();
datatable.Clear();

回答by HarshPritesh

datatable.Clear();
dataGridView1.DataSource = datatable;

回答by user3595795

Solution is:

解决办法是:

while (dataGridView1.RowCount > 1)
{
    dataGridView1.Rows.RemoveAt(0);
}

回答by valhalla

If you want to clear all the headers as well as the data, for example if you are switching between 2 totally different databases with different fields, therefore different columns and column headers, I found the following to work. Otherwise when you switch you have the columns/ fields from both databases showing in the grid.

如果您想清除所有标题和数据,例如,如果您在具有不同字段的 2 个完全不同的数据库之间切换,因此不同的列和列标题,我发现以下方法有效。否则,当您切换时,两个数据库中的列/字段都会显示在网格中。

dataTable.Dispose();//get rid of existing datatable
dataTable = new DataTable();//create new datatable

datagrid.DataSource = dataTable;//clears out the datagrid with empty datatable
//datagrid.Refresh(); This does not seem to be neccesary

dataadapter.Fill(dataTable); //assumming you set the adapter with new data               
datagrid.DataSource = dataTable; 

回答by Jon649

If it's bound to a datasource -

如果它绑定到数据源 -

dataGridView.DataSource=null;
dataGridView.Rows.Clear();

Worked for me.

为我工作。