Search code examples
c#listview

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

Solution

  • How about

    DataSource = null;
    DataBind();