Find specific value in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustRun
    New Member
    • Mar 2008
    • 127

    Find specific value in gridview

    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??
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    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.Ge tData 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.Selec t 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...

    Code:
    DataTable dt = new DataTable();
    foreach (DataRow dr in dt.Rows)
    {
      if (dr["quality"] <condition>)
      {
        dr["clmImg"] = <whatever>;
      }
    }
    Hopefully I got that right... and hopefully it helps. Good luck!

    Comment

    Working...