清除listview的所有项目 - c#

时间:2012-08-21 07:03:09

标签: c# .net listview

如何在C#中清除ListView?

以下是我填充ListView的方法:

int listViewCounter = 0;
for (int i = 0; i < dtXLS.Rows.Count; i++)
{
    listViewCounter++;

    ListViewItem item = new ListViewItem();
    item.Text = "First item" + listViewCounter;
    item.SubItems.Add("sadad" + listViewCounter);
    item.SubItems.Add("sadad" + listViewCounter);
    item.SubItems.Add("sadad" + listViewCounter);
    item.SubItems.Add("sadad" + listViewCounter);
    LV.Items.Add(item);

    LV.Items[listViewCounter - 1].SubItems[0].Text = listViewCounter.ToString();
    LV.Items[listViewCounter - 1].SubItems[1].Text = "sample1";
    LV.Items[listViewCounter - 1].SubItems[2].Text = "sample2";
    LV.Items[listViewCounter - 1].SubItems[3].Text = "sample3";
    LV.Items[listViewCounter - 1].SubItems[4].Text = "sample4";
}

结果将是:

1st Column    2nd Column    3rd Column    4th Column    5th Column
1             sample1       sample2       sample3       sample4

Cdeez这是我的代码的一部分,所以你可以看到:

public void ProcessFunction(RichTextBox rtb1, RichTextBox rtb2, DataTable dtXLS, ListView LV)
{
    int matchlist_counter = 0;


    string[] wordsToRedact = new string[dtXLS.Rows.Count];


    for (int i = 0; i < dtXLS.Rows.Count; i++)
    {
        wordsToRedact_counter++;


        wordsToRedact[i] = dtXLS.Rows[i][0].ToString();
        wordsToRedact[i] = Regex.Escape(dtXLS.Rows[i][0].ToString());


        Regex test = new Regex(@"\b(" + wordsToRedact[i] + @")\b", RegexOptions.Singleline | RegexOptions.Compiled);
        MatchCollection matchlist = test.Matches(rtb1.Text);


        if (matchlist.Count > 0)
        {
            for (int j = 0; j < matchlist.Count; j++)
            {
                WordsToRedact words = new WordsToRedact(matchlist[j]);

                HighLighting HL = new HighLighting();
                HL.Highlight_Words(pdfRT, words, Color.Yellow);
                matchlist_counter++;
            }
        }



        ListViewItem item = new ListViewItem();
        item.Text = "First item" + wordsToRedact_counter;
        item.SubItems.Add("sadad" + wordsToRedact_counter);
        item.SubItems.Add("sadad" + wordsToRedact_counter);


        LV.Items.Add(item);
        LV.Items[wordsToRedact_counter - 1].SubItems[0].Text = wordsToRedact_counter.ToString();
        LV.Items[wordsToRedact_counter - 1].SubItems[1].Text = wordsToRedact[i];
        LV.Items[wordsToRedact_counter - 1].SubItems[2].Text = matchlist_counter.ToString();

        matchlist_counter = 0;
    }
}


private void analyzeButton_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    DataTable dtXLS = loadXLS(xls_path);
    WordsToRedactFunc(pdfRT, visfRT, dtXLS, listView1);
    MessageBox.Show("Processing done!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

我现在的问题是每次点击特定按钮时如何清除所有项目?

5 个答案:

答案 0 :(得分:6)

更改您的代码:

ListViewItem item = new ListViewItem("First item" + listViewCounter);
item.SubItems.Add("sadad" + listViewCounter);
item.SubItems.Add("sadad" + listViewCounter);
item.SubItems.Add("sadad" + listViewCounter);
item.SubItems.Add("sadad" + listViewCounter);
LV.Items.Add(item);

点击按钮,点击此处LV.Items.Clear();

答案 1 :(得分:1)

您可以使用以下方法清除列表:

listView1.Items.Clear();

要添加一些新项目,请考虑

listView1.Items.Add(new ListViewItem(new string[] { string1 , string2 , ....}));

答案 2 :(得分:0)

对于所有会遇到同样问题的人,我现在弄清楚我的代码有什么问题,我只想在这里发布,

private void analyzeButton_Click(object sender, EventArgs e)
{
    listView1.Items.Clear();
    //I just add this line 
    listviewItem_Counter = 0;  // -> this integer needs to be set as zero because evrytime the analyze button be click it will be use, so it needs to be fresh again!

    DataTable dtXLS = loadXLS(xls_path);
    WordsToRedactFunc(pdfRT, visfRT, dtXLS, listView1);
    MessageBox.Show("Processing done!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

答案 3 :(得分:0)

LV.Columns.Clear()将清除所有列,包括标题。

答案 4 :(得分:-1)

只需使用 LV.Clear() 即可。 这将从控件中删除所有项目和列。