I try to clear my listview but the clear method doesn\'t work:
myListView.Items.Clear();
This doen\'t work. When i put a breakpoint at this
Don't bother with Clear(). Just do this: ListView.DataSource = null; ListView.DataBind();
The key is the databind(); Works everytime for me.
Try this ...
myListView.DataSource = null;
myListView.Items.Clear();
I would suggest to remove the rows from the underlying DataTable, or if you don't need the datatable anymore, set the datasource to null.
Probably your code works but it is rebound somewhere after you clear it. Make sure that this it not the case. It will be more helpful if you provide some code. Where are you setting your data source? Where are you data binding? Where are you clearing the list?
My guess is that Clear()
causes a Changed
event to be sent, which in turn triggers an automatic update of your listview from the data source.
So this is a feature, not a bug ;-)
Have you tried myListView.Clear()
instead of myListView.Items.Clear()
? Maybe that works better.
Just use the clear method is works like a charm. ListView1.Items.Clear() i think if its not working it may be the position whereby you place this code. Also can try nullifying the datasource.