RSS Forums RSS

Format single Row in datagridview using a value in same row

Please support our C# advertiser: DiscountASP.NET – 3 Months Free on C# Web Hosting
Reply
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Solved Threads: 0
binteron binteron is offline Offline
Newbie Poster

Question Format single Row in datagridview using a value in same row

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 140
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 2
Barefootsanders Barefootsanders is offline Offline
Junior Poster

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

  #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.

private void checkForSurveyColour(DataGridView dv) {
  foreach (DataRow dr in dv.Rows) {
    if(dr.Cells["RepVisits "].ToString().Equals("Fair") || dr.Cells["RepVisits "].ToString().Equals("Poor") {
      dr.Cells["RepVisits"].Style.ForeColor = Color.Red;
}
}

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 4:42 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 12037 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:01 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC