简体   繁体   中英

how to clear gridview

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..

dataGridView1.DataSource = null;

要么

dataGridView1.Rows.Clear();
gridview.DataSource = null;
//rebind to gridview
gridview.DataBind();

Bind the Gridview to an empty list.

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 )

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).

grdiview1.DataSource = new DataTable();
grdiview1.DataBind();

dataGridView1.Columns.Clear(); //this clears the entire Gridview

Simply add the following c# code to clear the GridView:-

gridView.Rows.Clear();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM