Format single Row in datagridview using a value in same row

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Solved Threads: 0
binteron binteron is offline Offline
Newbie Poster

Format single Row in datagridview using a value in same row

 
0
  #1
Sep 5th, 2007
I have a simple database that displays information in various datagridview controls on the same form. One thing I would like to do is change the colour of a row if the value from a cell returns "poor", or "fair"

I call a private function to go through each row in the datagrid just after it is connected to the bingingSource. I can get it to change the colour, but it formats all the rows with that colour, and I want only the row that returned the value to be changed.

  1. private void checkForSurveyColour()
  2. {
  3. foreach (DataGridViewRow row in dataGridView1.Rows)
  4. {
  5. try
  6. {
  7. CNumColour = dataGridView1.CurrentRow.Cells["RepVisits"].FormattedValue.ToString();
  8. if (CNumColour != null)
  9. {
  10. foreach(DataGridViewCell cells in row.Cells)
  11. {
  12. if (CNumColour == "Fair")
  13. {
  14. cells.Style.ForeColor = Color.Pink;
  15. }
  16. else if (CNumColour == "Poor")
  17. {
  18. cells.Style.ForeColor = Color.Red;
  19. }
  20. }
  21. }
  22.  
  23.  
  24. }
  25. catch(System.Exception ex)
  26. {
  27.  
  28. }
  29. }
  30.  
  31.  
  32. }

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Format single Row in datagridview using a value in same row

 
0
  #2
Sep 6th, 2007
This might help. You shouldnt have to check if its null because if its not equal to Fair or Poor it wont matter. Also, as long as you have a column within the dataview named RepVisits and you know it will be there ever single time, you dont need the try/catch.

  1. private void checkForSurveyColour(DataGridView dv) {
  2. foreach (DataRow dr in dv.Rows) {
  3. if(dr.Cells["RepVisits "].ToString().Equals("Fair") || dr.Cells["RepVisits "].ToString().Equals("Poor") {
  4. dr.Cells["RepVisits"].Style.ForeColor = Color.Red;
  5. }
  6. }
That is much more compact and I believe it should work the same way. The syntax might not be exactly how it should be but if you use visual studio and use the CTL + SPACE shortcuts after puting a period, it helps a lot in finding the proper method/properties you need.

Hope it helped.
Last edited by Barefootsanders; Sep 6th, 2007 at 5:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC