WinForm C#高效搜索DataGridView(未绑定)

时间:2013-03-05 23:52:18

标签: c# winforms datagridview

我有一个使用dataGridGrid的C#winForm,我每秒接收大约20个消息,我有大约1000行...是否有任何“快速查找”方法和/或设计模式,这将允许我找不到特定的行而不迭代dataGridView.Rows集合?这似乎是一种非常低效的方法,但我似乎找不到除了dataGridView.Rows.Remove()以外的任何其他东西,我“认为”是一个循环,我是否正确?有人可以帮帮我吗?

提前致谢,

-DA

2 个答案:

答案 0 :(得分:0)

你可能会使用一些LINQ来查找行,因为它是未绑定的。我不知道你对的是什么,但希望这可能会有所帮助:

var x = (from DataGridViewRow r in dataGridGrid.Rows 
         where r.Cells[SomeCellIndex_OrName].Value == "Some Value"
         select r).FirstOrDefault();

if (x != null ) {

  //Do Something to x
  // x is your row
  // x == null when not found
}

答案 1 :(得分:0)

如果我正确理解了您的问题,您希望在DataGridView中找到特定的行并将其删除。 假设您正在使用DataGridView尝试将其DataSource绑定到BindingSource,然后您可以找到(最后添加的)行,如下所示:

    BindingSource.Position = BindingSource.Find(string PropertyName, object key);

要删除所选行,请将您的位置保存到变量,然后:

    DataGridView.Rows.RemoveAt(your variable);

希望有所帮助

相关问题