簡體   English   中英

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

[英]WinForm C# Efficient Search of DataGridView (unbound)

我有一個使用dataGridGrid的C#winForm,並且每秒接收大約20 msgs的消息,我大約有1000行...是否有任何“快速查找”方法和/或設計模式將允許我定位特定行沒有迭代通過dataGridView.Rows集合? 這似乎是一種非常低效的方法,但是我似乎找不到dataGridView.Rows.Remove()以外的其他東西,而我“認為”這是一個循環,對嗎? 有人可以幫我嗎?

提前致謝,

-DA

您可能可以使用一些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
}

如果我正確理解了您的問題,則希望在DataGridView中查找特定的行並將其刪除。 假設您使用的是DataGridView,請嘗試將其DataSource綁定到BindingSource,然后可以找到(最后添加的)行,如下所示:

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

要刪除選定的行,請將位置保存到變量中,然后:

    DataGridView.Rows.RemoveAt(your variable);

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM