How to clear data from list view in c#

前端 未结 8 770
一个人的身影
一个人的身影 2021-01-07 08:32

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

8条回答
  •  不思量自难忘°
    2021-01-07 09:05

    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

提交回复
热议问题