Home > DeveloperSection > Forums > How to Give Gridview cell font colour according to condition
Goti Bandu

Total Post:119

Posted on    January-14-2015 10:03 PM

 ASP.Net C#  Gridview 

 1 Reply(s)
 814  View(s)
Rate this:

How can I give colour to gridview's cell text according to condition. I have a below gridview and I am taking data in gridview by code behind.

 <asp:GridView ID="GridView3" runat="server" AllowPaging="true" PageSize="5"

   AutoGenerateColumns="false"  Width="100%" OnPageIndexChanging="GridView3_PageIndexChanging"


                   <RowStyle CssClass="GridRow"/>


                         <asp:BoundField HeaderText="No" DataField="id" Visible="false"/>

                         <asp:BoundField HeaderText="Scenario" DataField="Scenario"/>

                         <asp:BoundField HeaderText="Type" DataField="Type"/>

                         <asp:BoundField HeaderText="Station Name" DataField="StationName"/>

                         <asp:BoundField HeaderText="Parameter" DataField="PARAM"/>

                         <asp:BoundField HeaderText="Value" DataField="Value"

                         SortExpression="Value" DataFormatString="{0:F2}"/>


                     <PagerStyle BackColor="White" Height="40px" Font-Bold="true" Font-

                      Size="Medium" ForeColor="Green" HorizontalAlign="Center"/>

                      <PagerSettings FirstPageText="First" LastPageText="Last"

                      Mode="NumericFirstLast" PageButtonCount="3" />

                      <HeaderStyle BackColor="#ABDB78" ForeColor="Black" Height="35px" Font-

                      Size="13px" Font-Names="verdana"/>


I am binding data with this grid by code behind.

   protected void ReservGridBind()


        string name = Request.QueryString[1].ToString();


        string query = "SELECT SD.id,SD.Scenario,SD.Value,PR.Type,PR.StationName,PR.PARAM

        from sgwebdb.param_reference as PR Inner join sgwebdb.scenario_data as SD

        ON PR.Param_Id=SD.Param_Id INNER JOIN sgwebdb.qualicision_detail as Q

        ON SD.SCENARIO=Q.Alternative where PR.Type='Reservoirs' and Q.Alternative='" + name +"'";


        this.GridView1.DataSource = PSI.DataAccess.Database.DatabaseManager




    condition :

    if  number < 0.0 then blue colour ,

     number > 0.0 && text < 4.0 then green colour,

      number > 4.0 then red colour.


    Please help me for this gridview's cell text colour.


Jeet Verma
Jeet Verma

Total Post:110

Posted on    January-14-2015 10:35 PM


<asp:GridView ID="GridView3" runat="server"

    AllowPaging="true" PageSize="5"






Code Behind:

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)


     if (e.Row.RowType == DataControlRowType.DataRow)


         int iText =  Convert.ToInt32(e.Row.Cells[4].Text);


         if (iText < 0)

             e.Row.Cells[4].ForeColor = System.Drawing.Color.Blue;            

         else if (iText > 0 && iText < 4)             

             e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;

         else if (iText > 4)          

             e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;           




Don't want to miss updates? Please click the below button!

Follow MindStick