43

I want to change a particular row color of a gridview based on some condition. I am using ASP.NET with C#.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Sunethpiumal
  • 621
  • 2
  • 9
  • 14

8 Answers8

52
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Attributes.Add("style", "cursor:help;");
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
    { 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {                
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");
            e.Row.BackColor = Color.FromName("#E56E94");                
        }           
    }
    else
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");
            e.Row.BackColor = Color.FromName("gray");                
        }           
    }
}
  • 4
    Instead of modifying `Row` attributes etc. it's much better to create to two css classes and chenge only `e.Row.CssClass `. Simpler code end better separation of logic and view. – Piotr Nawrot Dec 14 '15 at 11:43
29
protected void DrugGridView_RowDataBound(object sender, GridViewRowEventArgs e)

{
    // To check condition on integer value
    if (Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Dosage")) == 50)
    {
      e.Row.BackColor = System.Drawing.Color.Cyan;
    }
}
Mubashir Ahmed
  • 1,191
  • 9
  • 4
  • Thanks for the `DataBinder.Eval(arg1, arg2)` suggestion. Before this I was using a `HiddenField` to get the condition – Jamshaid K. Jan 08 '19 at 12:05
10

Create GridView1_RowDataBound event for your GridView.

//Check if it is not header or footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
    //Check your condition here
    If(Condition True)
    {
        e.Row.BackColor = Drawing.Color.Red // This will make row back color red
    }
}
Altaf Patel
  • 1,351
  • 1
  • 17
  • 28
ashish.chotalia
  • 3,696
  • 27
  • 28
4

This method modifies both the back color (to dark red) and the text (to white) if a specific string ("TextToMatch") occurs in one of the columns:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells[8].Text.Equals("TextToMatch"))
    {
        e.Row.BackColor = System.Drawing.Color.DarkRed;
        e.Row.ForeColor = System.Drawing.Color.White;
    }
}

Or another way to write it:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells[8].Text.Equals("TextToMatch"))
    {
        e.Row.Attributes.CssStyle.Value = "background-color: DarkRed; color: White";
    }
}
Crazy Cat
  • 1,332
  • 15
  • 19
2

Alternatively, you can cast the row DataItem to a class and then add condition based on the class properties. Here is a sample that I used to convert the row to a class/model named TimetableModel, then in if statement you have access to all class fields/properties:

protected void GridView_TimeTable_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var tt = (TimetableModel)(e.Row.DataItem);
                     if (tt.Unpublsihed )
                       e.Row.BackColor = System.Drawing.Color.Red;
                     else
                       e.Row.BackColor = System.Drawing.Color.Green;
                }
            }
        }
Amir978
  • 857
  • 3
  • 15
  • 38
1
 protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl_Code = (Label)e.Row.FindControl("lblCode");
            if (lbl_Code.Text == "1")
            {
                e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#f2d9d9");
            }
        }
    }
Code
  • 679
  • 5
  • 9
0

For the future programmer, if you didn't get the solution yet. The following code works for me to change the row color depending on condition (different types of the row that contain the string value):

public YourClassName()
{
   InitializeComponent();
   SetAlternateRowColor();
}

public void SetAlternateRowColor()
{
   for (int iCount = 0; iCount < reservationDataGridView.Rows.Count; iCount++)
   {
      if (Convert.ToString(reservationDataGridView.Rows[iCount].Cells[7].Value) == "CellValue")
         reservationDataGridView.Rows[iCount].DefaultCellStyle.BackColor = Color.Orange;
      else if (Convert.ToString(reservationDataGridView.Rows[iCount].Cells[7].Value) == "CellValue")
         reservationDataGridView.Rows[iCount].DefaultCellStyle.BackColor = Color.GreenYellow;
      else
         reservationDataGridView.Rows[iCount].DefaultCellStyle.BackColor = Color.Red;
   }
}
radipu
  • 41
  • 1
  • 9
-3
\\loop throgh all rows of the grid view  

if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value1")
{
   GridView1.Rows[i - 1].ForeColor = Color.Black;
}
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value2")
{
   GridView1.Rows[i - 1].ForeColor = Color.Blue;
}
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value3")
{
   GridView1.Rows[i - 1].ForeColor = Color.Red;
}
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value4")
{
   GridView1.Rows[i - 1].ForeColor = Color.Green;
}
Nick Pearce
  • 720
  • 5
  • 14
moataz
  • 11