Search code examples
c#listviewcolumnheader

How to delete the contents in List view, maintaining the frame(Windows Forms C# )


I've just started to learn basic programming and got an assignment to program a search box.

The problem I have is that the clear button clears the whole thing in the list view box including column headers. I want just the printed contents gone, and those column headers to remain. Here's my code.

private void button2_Click_1(object sender, EventArgs e)
    {
        Action<Control.ControlCollection> func = null;

        func = (controls) =>
        {
            foreach (Control control in controls)
                if (control is ListView)
                    (control as ListView).Clear();
                else
                    func(control.Controls);
        };

        func(Controls);
    }

Thanks for reading!


Solution

  • Use Items.Clear();

    Like this : listview.Items.Clear();

    Hope this works :)