title: Set individual cell BackColor based on cell content in c#
body: <p>I am inexperienced with c# and I am trying to change the background color of gridview cells based on their content.  I want multiple cells in the same row to be able to be different colors.  The gridview is generated fine but the colors are not applied.  I am using the below method and calling it when the gridview is created:</p>
  
  <pre><code>protected void cell_Color()
  {
      for (int r = 0; r &lt; gv.Rows.Count; r++)
      {
          for (int c = 0; c &lt; gv.Columns.Count; c++)
          {
              switch (gv.Rows[r].Cells[c].Text)
              {
                  case "A+":
                      gv.Rows[r].Cells[c].BackColor = Color.FromArgb(0, 255, 0);
                      break;
                  case "A":
                      gv.Rows[r].Cells[c].BackColor = Color.FromArgb(100, 255, 100);
                      break;
                  case "B":
                      gv.Rows[r].Cells[c].BackColor = Color.FromArgb(0, 0, 255);
                      break;
                  case "C":
                      gv.Rows[r].Cells[c].BackColor = Color.FromArgb(255, 255, 25);
                      break;
                  case "D":
                      gv.Rows[r].Cells[c].BackColor = Color.FromArgb(128, 64, 0);
                      break;
                  case "F":
                      gv.Rows[r].Cells[c].BackColor = Color.FromArgb(255, 0, 0);
                      break;
              }
          }
      }
  }
  </code></pre>
  
  <p>Edit1:
  Thanks for the help.  I found that one reason the cells weren't updating is because something in the gridview is preventing an exact match.  Since I can't find out what it is I just made strings for what I am looking for and used string.contain checking to see if they were true.  This helped me find a match, however it is only updating the first column.  I am using auto-generated columns so I cant use gv.columns.count so instead I just picked 12 since that is the most columns I use for the gridview.  The code now looks like</p>
  
  <pre><code>protected void RowDataBound(Object sender, GridViewRowEventArgs e)
  {
      string sAplus = "A+";
      string sA = "A";
      string sB = "B";
      string sC = "C";
      string sD = "D";
      string sF = "F";
  
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          for (int r = 1; r &lt;= gv.Rows.Count; r++)
          {
              if (e.Row.RowIndex == r)
              {
                  string grade = e.Row.Cells[0].Text;
                  bool bAplus = grade.Contains(sAplus);
                  bool bA = grade.Contains(sA);
                  bool bB = grade.Contains(sB);
                  bool bC = grade.Contains(sC);
                  bool bD = grade.Contains(sD);
                  bool bF = grade.Contains(sF);
                  if (bAplus == true)
                      e.Row.Cells[0].BackColor = Color.FromArgb(0, 255, 0);
                  if (bA == true)
                      e.Row.Cells[0].BackColor = Color.FromArgb(100, 255, 100);
                  if (bB == true)
                      e.Row.Cells[0].BackColor = Color.FromArgb(0, 0, 255);
                  if (bC == true)
                      e.Row.Cells[0].BackColor = Color.FromArgb(255, 255, 25);
                  if (bD == true)
                      e.Row.Cells[0].BackColor = Color.FromArgb(128, 64, 0);
                  if (bF == true)
                      e.Row.Cells[0].BackColor = Color.FromArgb(255, 0, 0);
              }
          }
      }
  }
  </code></pre>
  
  <p>Because I am using auto-generated columns, if I try and mace a variable for Cells and loop it like
      for(int c = 0; c> gv.Columns.Count; c++)
  it says the specified argument was out of the range of the valid values.</p>