943,708 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 29118
  • C# RSS
Sep 5th, 2007
0

Format single Row in datagridview using a value in same row

Expand Post »
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.

c# Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
binteron is offline Offline
7 posts
since Jun 2006
Sep 6th, 2007
0

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

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.

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: how to call C# method in C++
Next Thread in C# Forum Timeline: Help with a login program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC