Reputation: 24789
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 line, the line is executed, but my listview isn't empty. How come??
I fill my listview by setting it's datasource to a datatable.
My solution now is to set the datasource to an empty datatable.
I just wonder why clear don't do the trick?
I use a master page. Here some code of a content page when a button is pressed. The method SearchTitle fills the ListView.
Relevant code:
protected void Zoek()
{
// Clear listbox
ListView1.DataSource = new DataTable();
ListView1.DataBind();
switch (ddlSearchType.SelectedValue)
{
case "Trefwoorden":
SearchKeyword();
break;
case "Titel":
SearchTitle();
break;
case "Inhoud":
SearchContent();
break;
}
}
Method that fills the ListView
private void SearchTitle()
{
// Make panel visible
pnlResult.Visible = true;
pnlKeyword.Visible = false;
Search Search = new Search(txtSearchFor.Text);
ListView1.DataSource = Search.SearchTitle();
ListView1.DataBind();
}
Upvotes: 31
Views: 132509
Reputation: 29
The Problem is arising because you are trying to clear the entire list box.
Just use listView1.Items.Clear();
Upvotes: 2
Reputation:
I did a search on this and I am using WPF c#. Just in case you got here too looking for a WPF solution use the following:
yourlistview.ItemsSource = null;
Upvotes: 7
Reputation: 2341
This is bit late, but this works for me at least using UWP
myListView.ItemsSource = null;
Upvotes: 2
Reputation: 51
listView.Items.Clear()
listView.Refresh()
/e Updating due to lack of explanation. Often times, Clear() isn't suffice in the event of immediate events / methods following. It's best to update the view with Refresh() following a Clear() for an instant reflection of the listView clearing. This, anyhow had solved my related issues.
Upvotes: 3
Reputation: 37
Try with this:
myListView.ItemsSource = new List< DictionaryEntry >();
Upvotes: 1
Reputation: 81
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.
Upvotes: 0
Reputation:
Don't bother with Clear(). Just do this: ListView.DataSource = null; ListView.DataBind();
The key is the databind(); Works everytime for me.
Upvotes: 2
Reputation: 23766
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?
Upvotes: 0
Reputation: 580
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.
Upvotes: 0
Reputation: 20271
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.
Upvotes: 4
Reputation: 754745
Try this ...
myListView.DataSource = null;
myListView.Items.Clear();
Upvotes: 23