[c#] C# Clear all items in ListView

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();
    }

This question is related to c# listview

The answer is


Try this ...

myListView.DataSource = null;
myListView.Items.Clear();

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.


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?


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.


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;


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.


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?


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.


Try this ...

myListView.DataSource = null;
myListView.Items.Clear();

This is bit late, but this works for me at least using UWP

myListView.ItemsSource = null;

The Problem is arising because you are trying to clear the entire list box. Just use listView1.Items.Clear();


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.


Try with this:

myListView.ItemsSource = new List< DictionaryEntry >();

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.


The Problem is arising because you are trying to clear the entire list box. Just use listView1.Items.Clear();


Try with this:

myListView.ItemsSource = new List< DictionaryEntry >();

Don't bother with Clear(). Just do this: ListView.DataSource = null; ListView.DataBind();

The key is the databind(); Works everytime for me.


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.


Try this ...

myListView.DataSource = null;
myListView.Items.Clear();

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.


This is bit late, but this works for me at least using UWP

myListView.ItemsSource = 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?


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();

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.


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?