Click here to Skip to main content
11,577,983 members (63,524 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL C#3.5
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 21-Jul-14 23:54pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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);
       }
   }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Member239258 at 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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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())%>
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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)
            { }
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Default.aspx
<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;

}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 165
1 Richard MacCutchan 165
2 Peter Leow 160
3 Afzaal Ahmad Zeeshan 125
4 Frankie-C 85
0 OriginalGriff 880
1 Sergey Alexandrovich Kryukov 685
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 393


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 22 Jul 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100