User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,523 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,513 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 5881 | Replies: 1
Reply
Join Date: Jun 2006
Posts: 7
Reputation: binteron is an unknown quantity at this point 
Rep Power: 0
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  
Join Date: Oct 2006
Posts: 128
Reputation: Barefootsanders is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
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.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 6:58 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC