0

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:

protected void cell_Color()
{
    for (int r = 0; r < gv.Rows.Count; r++)
    {
        for (int c = 0; c < 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;
            }
        }
    }
}

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

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 <= 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);
            }
        }
    }
}

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.

Andrew O'Brien
  • 1,793
  • 1
  • 12
  • 24

3 Answers3

0

Call your function on PreRender.
All the controls on the page are created and populated with content by then.
It is the best place to make data related color decisions.

nunespascal
  • 17,584
  • 2
  • 43
  • 46
0

You can use RowDataBound event of grid view to set different colors when it is generated.

Enable RowDataBound Event in the markup.

<asp:GridView ID="gridview1" runat="server" OnRowDataBound="RowDataBound">

Write this code in code-behind file:

protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {

    if(e.Row.RowIndex == 0)     // This is row no.1
        if(e.Row.Cells[0].Text == "ABC")
            e.Row.Cells[0].BackColor = Color.Red;

    if(e.Row.RowIndex == 1)     // This is row no.2
        if(e.Row.Cells[0].Text == "CBA")
            e.Row.Cells[0].BackColor = Color.Green;
    }
}

Read more here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Aung Kaung Hein
  • 1,516
  • 5
  • 24
  • 40
  • Thanks for the help. I updated my post with where I am at now. The short of it is I can't get this to update every cell in the row because I am using auto-generated columns. – Andrew O'Brien Jul 02 '13 at 06:13
0

I found the answer. I couldn't use RowDataBound, I believe because I was using auto-generated columns and trying to update any cell above 0 returned an error. Since I am using a method to generate the gridview, I updated that to call the original cell_Color() method at the end and used string.contain instead of looking for an exact match. I started at column 1 since I don't want to highlight the first column.

protected void cell_Color()
{
    string sAplus = "A+";
    string sA = "A";
    string sB = "B";
    string sC = "C";
    string sD = "D";
    string sF = "F";
    for (int r = 0; r < gv.Rows.Count; r++)
    {
        for (int c = 1; c < gv.Rows[r].Cells.Count; c++)
        {
            string grade = gv.Rows[r].Cells[c].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)
                    gv.Rows[r].Cells[c].BackColor = Color.FromArgb(0, 255, 0);
                if (bA == true)
                    gv.Rows[r].Cells[c].BackColor = Color.FromArgb(100, 255, 100);
                if (bB == true)
                    gv.Rows[r].Cells[c].BackColor = Color.FromArgb(0, 0, 255);
                if (bC == true)
                    gv.Rows[r].Cells[c].BackColor = Color.FromArgb(255, 255, 25);
                if (bD == true)
                    gv.Rows[r].Cells[c].BackColor = Color.FromArgb(128, 64, 0);
                if (bF == true)
                    gv.Rows[r].Cells[c].BackColor = Color.FromArgb(255, 0, 0);
            }
        }
    }
}

If I wasn't using auto-generated columns I think I would prefer using RowDataBound instead.

Thanks for your help.

Andrew O'Brien
  • 1,793
  • 1
  • 12
  • 24