How to clear data from list view in c#

前端 未结 8 754
一个人的身影
一个人的身影 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:03

    While making windows store app write:

    listview.Items.Clear();
    

    Just listview.Clear(); won't work in windows store app

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-07 09:07

    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

    0 讨论(0)
  • 2021-01-07 09:09
        foreach (ListView item in listview.Items)
        {
            ListView.Items.Remove(item);
        }
    

    or

        ListView.Clear();
    
    0 讨论(0)
  • 2021-01-07 09:15

    What about the following?

    ListView lv = new ListView();
    while (lv.Items.Count > 1) {
        //leave the header
        lv.Items.RemoveAt(1);
    }
    
    0 讨论(0)
  • 2021-01-07 09:16

    In WinForms:

    listView1.Clear();
    

    In WPF:

    listView1.Items.Clear();
    
    0 讨论(0)
提交回复
热议问题