C# 如何清空数据网格视图?

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

How to empty a datagridview?

c#winformsdatagridview

提问by Harvey

I don't know what is the syntax for emptying a datagridview. Please help me here is my code.

我不知道清空 datagridview 的语法是什么。请帮我这是我的代码。

if (cboProduct.SelectedIndex != -1)
    load_variant();
else
    //empty the datagridview
    cboProduct.SelectedIndex = -1;

采纳答案by Damith

set datasource as null

将数据源设置为空

dataGridView1.DataSource = null;

Or

或者

dataGridView1.Rows.Clear()

OR

或者

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

回答by Habib

syntax for emptying a datagridview

清空数据网格视图的语法

Just assign nullto its DataSource property.

只需分配null给它的DataSource 属性

yourGridView.DataSource = null;

回答by Soner G?nül

Just set DataGridView.DataSourceproperty to null

只需将DataGridView.DataSource属性设置为null

Gets or sets the data source that the DataGridView is displaying data for.

获取或设置 DataGridView 为其显示数据的数据源。

DataGridView1.DataSource = null;

As an alternative (not exactly what .DataSource = nulldoes)

作为替代方案(不完全是什么.DataSource = null

DataTable dt = (DataTable)DataGridView1.DataSource;
if(dt != null)
    dt.Clear();

回答by Maryam Arshi

You can set its DataSource to null :

您可以将其 DataSource 设置为 null :

dataGridView.DataSource = null;

回答by Freelancer

Approach 1:

方法一:

datagridview1.DataSourse=null;

Approach2:

方法二:

DataView DV = (DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DV.Table.Clear();

Approach 3:

方法三:

datagridview1.DataSource = ""

Approach 4:

方法四:

datagridview1.Dispose();//Clears gridview with all its properties

Approach 5:

方法五:

Using Javascript:

使用 JavaScript:

document.getElementById("datagridview1").outerHTML = "";

Hope Its Helpful.

希望它有帮助。

回答by Hyman

Try this:

尝试这个:

int nn = yourgridview.Rows.Count;
for (int i=nn; i>1; i--)
{ 
    {
        yourgridview.Rows.RemoveAt(i-2);
    }
}

回答by JayByte

Setting the data source to null will also reset all formatting of the DataGridView. You can preserve the formatting by creating an empty DataTableand setting that as the source.

将数据源设置为 null 也会重置DataGridView. 您可以通过创建一个空DataTable并将其设置为源来保留格式。

For example:

例如:

// Create an empty datatable
DataTable empty = new DataTable();
empty.Columns.Add("Name", typeof(string));
empty.Columns.Add("Color", typeof(myEnum));
empty.Columns.Add("Count", typeof(int));
...

// Clear datagridview
dgv.DataSource = empty;

回答by phanvugiap

if (dgView.DataSource == null) return; ((DataTable)dgView.DataSource).Rows.Clear(); ((DataTable)dgView.DataSource).Columns.Clear();

if (dgView.DataSource == null) return; ((DataTable)dgView.DataSource).Rows.Clear(); ((DataTable)dgView.DataSource).Columns.Clear();

I used this one to clean datagrid view on my window application.

我用这个来清理我的窗口应用程序上的数据网格视图。

dgView.DataSource = nullis not working for me

dgView.DataSource = null不适合我