防止重复在ListView中插入数据

时间:2015-06-17 08:09:25

标签: c# listview

这是我的代码:

ListViewItem lst1 = new ListViewItem();
lst1.SubItems.Add(txtKodeJob.Text);
lst1.SubItems.Add(gridLookUpEditJob.Text);
lst1.SubItems.Add(gridLookUpEditJF.Text);
lst1.SubItems.Add(txtQty.Text);
lst1.SubItems.Add(txtTarget.Text);
lst1.SubItems.Add(deStart.Text);
lst1.SubItems.Add(deEnd.Text);
lst1.SubItems.Add(txtDurasiHari.Text);
lst1.SubItems.Add(txtNotes.Text);
listView1.Items.Add(lst1);

如何防止C#中TextBox的重复数据?

1 个答案:

答案 0 :(得分:0)

ListView类提供了一些不同的方法来确定项是否存在:

在Items集合上使用Contains 使用FindItemWithText方法之一 它们可以按以下方式使用:

// assuming you had a pre-existing item
ListViewItem item = ListView1.FindItemWithText("test");
if (!ListView1.Items.Contains(item))
{
    // doesn't exist, add it
}

// or you could find it by the item's text value
ListViewItem item = ListView1.FindItemWithText("test");
if (item != null)
{
    // it exists
}
else
{
    // doesn't exist
}

// you can also use the overloaded method to match sub items
ListViewItem item = ListView1.FindItemWithText("world", true, 0);