Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Dear Friends,

Am working on asp.net Gridview using c#.


I want to change the Forecolor of complete Gridview row based on condition.

In database i have a column with Result

if Result is PASS make the gridview Forecolor to Green
and if the result is FAIL make the gridview Forecolor to RED.

waiting for ur answer.
thanks.
Posted
Updated 10-Jul-19 19:40pm

Create GridView_RowDataBound event for your GridView

if (e.Row.RowType = DataControlRowType.DataRow)
{
//Put your condition here
If(Condition True)
{
e.Row.BackColor = Drawing.Color.Red // This will make row back color red
}
}

or
you may play with conditional operator with this on aspx:
<%# System.Drawing.Color.FromName(Eval("BgColor").ToString())%>
 
Share this answer
 
v3
Here is the working code.

DataTable dt;
        
protected void Page_Load(object sender, EventArgs e)
        {
            GenerateData();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        private void GenerateData()
        {
            dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Result", typeof(string));

            DataRow dr;
            dr = dt.NewRow();
            dr["ID"] = "1";
            dr["Name"] = "Kumar";
            dr["Result"] = "Pass";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "2";
            dr["Name"] = "Sharvesh";
            dr["Result"] = "Fail";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["ID"] = "3";
            dr["Name"] = "Priyam";
            dr["Result"] = "Pass";
            dt.Rows.Add(dr);

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                GridViewRow grv = e.Row;
                if (grv.Cells[2].Text.Equals("Fail"))
                {
                    e.Row.BackColor = System.Drawing.Color.Red;
                }
                if (grv.Cells[2].Text.Equals("Pass"))
                {
                    e.Row.BackColor = System.Drawing.Color.Green;
                }
            }
            catch (Exception ex)
            { }
        }
 
Share this answer
 
C#
protected void GVpullout_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       try
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {

               DateTime dtTmp = DateTime.ParseExact(e.Row.Cells[5].Text, "dd/MM/yyyy", null);



               if (dtTmp > DateTime.Now)
               {
                   e.Row.BackColor = Color.FromName("#41A317");
               }
               else if (dtTmp == DateTime.Now.Date)
               {
                   e.Row.BackColor = Color.FromName("#F39110");

               }
               else
                   e.Row.BackColor = Color.FromName("#E33638");

           }
       }
       catch (Exception ex)
       {
           Log(ex.Message, ex.StackTrace);
           ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Exception Message", "alert('You have an exception,please consult IT department')", true);
       }
   }
 
Share this answer
 
Default.aspx
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate><table border="1" style="border-collapse:collapse"><tr><th>ID</th><th>Title</th><th>Message</th></tr></HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'/></td>
   <td><asp:Label ID="lbl_Msg" runat="server" Text='<%#Eval("Messge") %>'/></td>
    <td><asp:Label ID="lbl_Status" runat="server" Text='<%#Eval("Status") %>'/></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



Default.aspx.cs
string s = "data source=localhost;initial catalog=xxx;user id=sa;password=pas@123";
SqlConnection Conn = new SqlConnection(s);
Conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("Select * from t1", Conn);
DataTable Bind = new DataTable();
Adapter.Fill(Bind);
GridView1.DataSource = Bind;
GridView1.DataBind();
Conn.Close();

foreach (GridViewRow item in GridView1.Rows)
{
Label l = (Label)item.FindControl("lbl_Status");
if (l.Text == "Fail")
l.BackColor = System.Drawing.Color.Red;

}
 
Share this answer
 
Hi,

To Achieve this use the RowDataBound event for GridView control on codebehind page. In this event you can check for the column value if column value is Pass then color it with Green color, for Fail value color it with Red Color.
 
Share this answer
 
Comments
Member239258 22-Jul-14 6:02am    
Please can you help me, am fresher. i dont have idea.
i want to make the complete row color change not column.

Please help thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900