"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Saturday, October 9, 2010

gridview row color based on different values


depending up on x and y condition , changing the background color of the row

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // check your condition here
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string strValue = DataBinder.Eval(e.Row.DataItem, "SomeColumn").ToString();
        //do something with the values and set color
        switch (strValue)
        {
            // change row color
            case "Something 1":
                e.Row.BackColor = System.Drawing.Color.Red;
                break;
            case "Something 2":
                e.Row.BackColor = System.Drawing.Color.Yellow;
                break;
            case "Something 3":
                e.Row.BackColor = System.Drawing.Color.Violet;
                break;
            case "Something 4":
                e.Row.BackColor = System.Drawing.Color.Teal;
                break;
        }
    }
}


Have a nice day... 'N happy Coding :)

No comments: