Check DataGridView for existing row

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

Hello all,

I thought this would be simple, I have a DaraGridView with 200 or so
rows. All I want to do is check every row to see if a particular
string value exists in column[1]. What's the best way to achieve this?

I don't really want to do a foreach as there's about 100 + values to
compare.

Regards,

Jon
 
I thought this would be simple, I have a DaraGridView with 200 or so
rows. All I want to do is check every row to see if a particular
string value exists in column[1]. What's the best way to achieve this?

I don't really want to do a foreach as there's about 100 + values to
compare.

And what sort of magic do you think someone else can come up with besides
comparing values? Ultimately that's what's going to happen at SOME level.
Are you simply wanting to be shielded from the details or were you hoping
that there was actually a way that didn't involve comparisons?
 
If the values are in some data store, you can use LINQ to COUNT the number
of elements that are equal to the one you seek. Sure, behind the scene,
there will be either a loop, either an index search (or whatever, it really
depends on your data store), but you won't explicitly have a loop. On the
other hand, the comparison will be only against committed values in the data
store (by opposition to the one you may be editing and not yet stored).


Vanderghast, Access MVP
 
I thought this would be simple, I have a DaraGridView with 200 or so
rows. All I want to do is check every row to see if a particular
string value exists in column[1]. What's the best way to achieve this?
I don't really want to do a foreach as there's about 100 + values to
compare.

And what sort of magic do you think someone else can come up with besides
comparing values? Ultimately that's what's going to happen at SOME level.
Are you simply wanting to be shielded from the details or were you hoping
that there was actually a way that didn't involve comparisons?

No sort of magic,just by using a method built into the framework it
saves me having to re-invent the wheel.
 
If the values are in some data store, you can use LINQ to COUNT the number
of elements that are equal to the one you seek. Sure, behind the scene,
there will be either a loop, either an index search (or whatever, it really
depends on your data store), but you won't explicitly have a loop. On the
other hand, the comparison will be only against committed values in the data
store (by opposition to the one you may be editing and not yet stored).

Vanderghast, Access MVP




Hello all,
I thought this would be simple, I have a DaraGridView with 200 or so
rows. All I want to do is check every row to see if a particular
string value exists in column[1]. What's the best way to achieve this?
I don't really want to do a foreach as there's about 100 + values to
compare.

Jon

Vanderghast, thanks for the useful response.
 
Hi,

You could try something like this -- assuming that data for your
datagridview resides in a datatable:

DataRow[] drf = ds.Tables["tbl1"].Select("fld0 Like '*xx*'");
foreach (DataRow dr in drf)
Console.WriteLine(dr[0].ToString());

Another option would be to loop through the datagridview rows collection:

foreach(DataGridViewRow row in datagridview1.Rows)
Console.WriteLine(row[0].ToString());

you can add an if statement here if (row[0].ToString.Equal("xx")) ...

Rich
 
Hi,

You could try something like this -- assuming that data for your
datagridview resides in a datatable:

DataRow[] drf = ds.Tables["tbl1"].Select("fld0 Like '*xx*'");
foreach (DataRow dr in drf)            
      Console.WriteLine(dr[0].ToString());

Another option would be to loop through the datagridview rows collection:

foreach(DataGridViewRow row in datagridview1.Rows)
      Console.WriteLine(row[0].ToString());

you can add an if statement here if (row[0].ToString.Equal("xx")) ...

Rich



Jon said:
Hello all,
I thought this would be simple, I have a DaraGridView with 200 or so
rows. All I want to do is check every row to see if a particular
string value exists in column[1]. What's the best way to achieve this?
I don't really want to do a foreach as there's about 100 + values to
compare.

Jon
.

Thanks Rich, appreciate your post.

Jon
 
Back
Top