简体   繁体   中英

WinForm C# Efficient Search of DataGridView (unbound)

I have a C# winForm that uses the dataGridGrid and I'm receiving about 20 msgs per/sec and I have approx 1000 rows...is there any "fast lookup" methods and/or design pattern that will allow me to locate specific rows without iterating through dataGridView.Rows collection? This seems to be a VERY inefficient approach but I can't seem to find anything else other than dataGridView.Rows.Remove() which I "think" is a loop, am I correct? Can someone please help me out?

Thanks in advance,

-DA

you could probably use some LINQ to find the row, since it is unbound. I don't know what you're matching against, but hopefully this could help:

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
}

If I understood correctly your question, you want to find specific rows in your DataGridView and delete them. Assuming you are using DataGridView try to bind it's DataSource to a BindingSource and then you could find(last added) row like this:

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

To remove selected row, save your position to a variable and then:

    DataGridView.Rows.RemoveAt(your variable);

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM