Change cell color on different values – Gridview

I need to distinguish two consecutive cells.

Each one in a row, if they have different values, when databindind the values to a gridview.

So, if in the row 1 I have the cell “ABC” and in the row 2 I have the cell “CBA”.

I need to color each cell with a different color.

What is the best way to do it?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is called Conditional Formatting

You can enable the RowDataBound Event in the markup

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

</asp:GridView>

And put this in your 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;
    }
}

Method 2

Add to your gridview in html part of page OnRowDataBound=”gridView1_DataBinding”. And then add event handler codebehind:

protected void gridView1_DataBinding(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType != DataControlRowType.DataRow) return;

        var c = e.Row.FindControl("IdOfControl") as Label;
        if(c != null)
        {
            if (c.Text == "ABC")
                e.Row.BackColor = GetColor("Gray");

            if (c.Text == "BCA")
                e.Row.BackColor = GetColor("Green");
        }
    }

    private Color GetColor(string color)
    {
        return Color.FromName(color);
    }

Best regards, Dima.

Method 3

if i understand you right, you want to change the color of a cell, depending on it’s value.
if that’s correct, you could try it like this:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((Label)e.Row.Cells[0].FindControl("ValueHoldingControl").Text == "ABC")
        {
            //Coloring the cell
        }
    }
}

Method 4

you can do it on the rowdatabound event of the gridview. Keep the previous row in viewstate or session and match it with the next row. If it does’t match, change the color otherwise don’t change.

Method 5

void gvShowFullDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#AECD6F");
        }
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x