Friday 18 October 2013

How to Change Gridview background Row colour dynamically in asp.net & c#


In this article I will explain how to Change Gridview background Row colour dynamically in ASP.NET & C#.NET

Write the below code in GridViewRow data bound event.

Below code is very useful for conditions based.

 protected void gvNew_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Row.DataItem;
                 if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lblCategoryMain = (Label)e.Row.FindControl("lblCostCategory");
            if (lblCategoryMain != null)
            {
                if (lblCategoryMain.Text == "1")
                {
                                        e.Row.Cells[1].BackColor =   System.Drawing.Color.LightGray;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.Black;
                }
                if (lblCategoryMain.Text == "2")
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.Gray;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.Black;
                }
                if (lblCategoryMain.Text.Trim() == "3")
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.LightGreen;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.Black;
                }
                if (lblCategoryMain.Text == "4")
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.LightSeaGreen;
                    e.Row.Cells[1].ForeColor = System.Drawing.Color.Black;
                }
            }

        }

No comments:

Post a Comment