vb.net 如何清除datagridview中的所有数据并且不会影响删除数据

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

How to clear all data in datagridview and it would not effect in delete data

vb.netwinformsdatagridview

提问by HuBeZa

How do I clear all data and not delete data or column in datagridview? I want to clear the data first and query on the result to make the result not duplicate.

如何清除所有数据而不删除 datagridview 中的数据或列?我想先清除数据并查询结果以使结果不重复。

回答by HuBeZa

I'm not sure that I understand what you asked, but yet - Clearing all rows from DataGridViewis very simple:

我不确定我是否理解您的要求,但是 - 从DataGridView清除所有行非常简单:

dataGridView1.Rows.Clear();

Regarding the query duplicates, you can use SELECT DISTINCT. And another thing: I hope your code is only a simplified example, because it is highly exposed to SQL injections.

关于查询重复,您可以使用SELECT DISTINCT。还有一件事:我希望你的代码只是一个简化的例子,因为它高度暴露于SQL 注入

回答by Sujith H S

If you are binding your DataGridView through code that is, using DataSourceproperty you can clear DataGridView using the following code.

如果您通过代码绑定 DataGridView,即使用DataSource属性,您可以使用以下代码清除 DataGridView。

dataGridView1.DataSource=null;

dataGridView1.DataSource=null;

It will not delete data from your database,just clear your DataGridView.

它不会从您的数据库中删除数据,只需清除您的 DataGridView。

回答by ünêss ?làm?

in vb.net you can use :

在 vb.net 中,您可以使用:

datagridview1.Datasource = nothing

datagridview1.Datasource = 没有

but it will empty your datagridview1 and initialize it to default.

但它会清空您的 datagridview1 并将其初始化为默认值。

回答by BiscuitG

I had to modify Sujith's to the following:

我不得不将 Sujith 修改为以下内容:

DataGridView1.DataSource = DBNull.Value

DataGridView1.DataSource = DBNull.Value

回答by Serhat Ates

If datagrid is bound data you should do:

如果 datagrid 是绑定数据,你应该这样做:

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

If datagrid is unbound data no need to null the datasource:

如果数据网格是未绑定的数据,则无需将数据源设为空:

dataGridView.DataSource.Clear();

Another trick is:

另一个技巧是:

 dt.Columns.Clear();
 dt.Rows.Clear();
 dataGridView.DataSource = dt;