I have a button \"Show data\". When click it, data will display in list view. But I want to clear it and then click \"Show data\" again to display new data. Because I don\'t
While making windows store app write:
listview.Items.Clear();
Just listview.Clear(); won't work in windows store app
listView1.Items.Clear();
Clears in 2008 c# column headers with the data, if you have multiple ListView on the same form the best way is writing a method such as (suggested by Lasse as above)
private void ClearLvItems(ListView li)
{
while(li.Items.Count>1)
li.Items.RemoveAt(1);
}
Or if it does not work as expected as it does not work with me (one row still rests in Listview)
listView1.Items.Clear();
SetHeaders(li); // If you have more then one ListView in the same form. Otherwise don't use the parameters.
private void SetHeader(ListView li)
{
string[] header_names = new string[] {"Id","Name","SurName","Birth Date"};
int i = 0;
foreach (ColumnHeader ch in li.Columns)
{
ch.Text = header_names[i];
++i;
}
}
Another discussion is Here
ListView.Clear Method removes all items and columns from the control.
The following are some articles that may help you working with ListView and DataGridView controls:
ListView @ C# Online.Net
Working with Data—Using the DataGridView
foreach (ListView item in listview.Items)
{
ListView.Items.Remove(item);
}
or
ListView.Clear();
What about the following?
ListView lv = new ListView();
while (lv.Items.Count > 1) {
//leave the header
lv.Items.RemoveAt(1);
}
In WinForms:
listView1.Clear();
In WPF:
listView1.Items.Clear();