Connecting Tech Pros Worldwide Help | Site Map

Find specific value in gridview

JustRun's Avatar
Member
 
Join Date: Mar 2008
Posts: 109
#1: Aug 16 '09
Hi,

Well, I have a DataGridView binded to a TableAdapter and there is an extra empty column named "clmImg" to put an image or color in it.

I want to do the following :

search through the quantity column, if you find a value less than 15 then put a red color "or red image" in the empty column.

I don't know who to write the code. Any help??
Familiar Sight
 
Join Date: Jul 2009
Location: Calgary, Alberta, Canada
Posts: 211
#2: Aug 17 '09

re: Find specific value in gridview


All right... so it's been a while since I've worked with a DataGridView so bear with me. I had to google how you got data out of a TableAdapter, but it seems like it's straightforward.

According to the MSDN article I found, TableAdapter.GetData gets you a DataTable for your DataGridView... and that's good enough for me.

So now you're looking for matches on the "quantity" column and want to do something to the clmImg column. No problem, first you need to search the table for a match. There's likely a few ways to do this...

1. Look up how to run an SQL query on a DataTable... I believe you can do this, try googling.
2. Use the DataTable.Select command
3. Manually search through the DataTable to find what you want...

I'll go through the 3rd option with you. I'm willing to bet it's not optimal, but when you play around with these objects like this, you get a better idea of how to use them, which is always a plus.

Basically, just go through the DataTable's rows and look for what you want, then assign the column you want. So basically...

Expand|Select|Wrap|Line Numbers
  1. DataTable dt = new DataTable();
  2. foreach (DataRow dr in dt.Rows)
  3. {
  4.   if (dr["quality"] <condition>)
  5.   {
  6.     dr["clmImg"] = <whatever>;
  7.   }
  8. }
Hopefully I got that right... and hopefully it helps. Good luck!
Reply


Similar C# / C Sharp bytes