vb.net VB.Net清除没有标题的datagridview行

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

VB.Net clear datagridview rows without the headers

vb.netwinformsdatagridview

提问by codeGEN

I have a datagridview with columns added from the designer, the data for this grid will be selected from the database and will be directly bound to the grid. For this purpose I have DataPropertyName to the column names of the database table.

我有一个从设计器添加的列的数据网格视图,该网格的数据将从数据库中选择并直接绑定到网格。为此,我将 DataPropertyName 作为数据库表的列名。

I am setting the datasource like this :

我正在像这样设置数据源:

dgPayment.DataSource = myDatatable

Now I need to clear the rows of the datagridview without removing the headers of the datagridview. I tried using dgPayment.Rows.Clear(), but this prompted a error because the grid is bound & therefore the rows cannot be manually altered. I also tried setting the datasource to nothing like this :

现在我需要清除 datagridview 的行而不删除 datagridview 的标题。我尝试使用dgPayment.Rows.Clear(),但这提示错误,因为网格已绑定,因此无法手动更改行。我也尝试将数据源设置为这样的:

dgPayment.DataSource = Nothing

dgPayment.DataSource = Nothing

But this removes the headers too, which doesn't need to happen because they are the column headers that I added using the designer. How can I clear only the data without clearing the headers.

但这也会删除标题,这不需要发生,因为它们是我使用设计器添加的列标题。如何只清除数据而不清除标题。

回答by Fabio

If you create columns through designer, then in form constructoror in Loadevent handler put next line of code

如果您通过设计器创建列,则在表单constructorLoad事件处理程序中放置下一行代码

dgPayment.AutoGenerateColumns = false

Then freely use dgPayment.DataSource = Nothingfor removing all rows

然后自由dgPayment.DataSource = Nothing用于删除所有行

回答by Aseem Gautam

Remove only rows from the datatable.

仅从数据表中删除行。

DataTable.Rows.Clear(). This will keep the columns intact.

DataTable.Rows.Clear(). 这将保持列完好无损。

Then to refresh the data -

然后刷新数据 -

CurrencyManager cm = ((CurrencyManager)) dataGridView1.BindingContext[dataGridView1.DataSource];
cm.Refresh();

回答by Sathish

Try like this

像这样尝试

dataGridView1.DataSource.Rows.Clear()