C# 如何清除网格视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2256991/
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
how to clear gridview
提问by vishal
I want to clear my gridview. I have 2 GridViews and Has Select Button On It. on Selecting this button that item goes into the second gridview. now the question is how should i clear the second grid view. I am trying the clear method but clear method is not found in my visual studio..
我想清除我的网格视图。我有 2 个 GridViews 并且上面有选择按钮。选择此按钮,该项目将进入第二个网格视图。现在的问题是我应该如何清除第二个网格视图。我正在尝试清除方法,但在我的视觉工作室中找不到清除方法..
回答by Patrick Kafka
dataGridView1.DataSource = null;
or
或者
dataGridView1.Rows.Clear();
回答by nebula
gridview.DataSource = null;
//rebind to gridview
gridview.DataBind();
回答by Aniket Banerjee
Simply add the following c# code to clear the GridView:-
只需添加以下 c# 代码即可清除 GridView:-
gridView.Rows.Clear();
gridView.Rows.Clear();
回答by Inaldi Gomez
dataGridView1.Columns.Clear(); //this clears the entire Gridview
dataGridView1.Columns.Clear(); //这会清除整个Gridview
回答by Spacemonkey
Bind the Gridview to an empty list.
将 Gridview 绑定到一个空列表。
Binding it to 'null' like Patrick Kafka mentioned should work - unless you have some column requirements (I'm mentioning it because I have a tendancy to plug in javascript into my gridviews and unless you specify those columns in the markup, they won't be generated and it will cause errors in the js. (This is also relevant to those getting errors after doing Columns.Clear )
像 Patrick Kafka 提到的那样将它绑定到 'null' 应该可以工作 - 除非你有一些列要求(我提到它是因为我倾向于将 javascript 插入到我的 gridviews 中,除非你在标记中指定这些列,否则他们不会t 被生成,它会导致 js 中的错误。(这也与那些在执行 Columns.Clear 后出现错误有关)
In a case like that (as well as in all other cases), you can simply bind the gridview to a new instance (or empty instance) of your datasource. (Below example for a gridview bound to a datatable - it could be bound to new List<T>()
as well).
在这种情况下(以及在所有其他情况下),您可以简单地将 gridview 绑定到数据源的新实例(或空实例)。(下面是绑定到数据表的 gridview 示例 - 它也可以绑定到new List<T>()
)。
grdiview1.DataSource = new DataTable();
grdiview1.DataBind();