简体   繁体   中英

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

How about

DataSource = null;
DataBind();

Try this ...

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

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.

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

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

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

试试这个:

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

我建议从基础数据表中删除行,或者如果不再需要该数据表,则将数据源设置为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.

这有点晚了,但这至少对我有用UWP

myListView.ItemsSource = null;

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.

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