---
title: "How to Give Gridview cell font colour according to condition"  
description: "How to Give Gridview cell font colour according to condition"  
author: "Anonymous User"  
published: 2015-01-14  
updated: 2015-01-14  
canonical: https://www.mindstick.com/forum/12856/how-to-give-gridview-cell-font-colour-according-to-condition  
category: "asp.net"  
tags: ["c#", "gridview"]  
reading_time: 2 minutes  

---

# How to Give Gridview cell font colour according to condition

How can I give colour to [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net)'s cell [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) according to [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition). I have a below gridview and I am taking data in gridview by [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind).

```
 <asp:GridView ID="GridView3" runat="server" AllowPaging="true" PageSize="5"   AutoGenerateColumns="false"  Width="100%" OnPageIndexChanging="GridView3_PageIndexChanging"   CssClass="Grid">                   <RowStyle CssClass="GridRow"/>                     <Columns>                         <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}"/>                    </Columns>                     <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"/>                </asp:GridView>
```

I am [binding data](https://www.mindstick.com/forum/4/binding-data-with-datagridview) with this [grid](https://www.mindstick.com/forum/369/how-can-i-add-a-column-name-to-my-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                                    .GetConnection().GetData(query);        GridView1.DataBind();
```

**condition :**

if number < 0.0 then blue colour ,

number > 0.0 && text < 4.0 then [green](https://www.mindstick.com/articles/95829/think-green-sustainable-design-considerations-for-commercial-buildings) colour,

number > 4.0 then red colour.

Please help me for this gridview's cell text colour.

## Replies

### Reply by Anonymous User

**Markup**:

```
<asp:GridView ID="GridView3" runat="server" AllowPaging="true"PageSize="5"    AutoGenerateColumns="false" Width="100%"    OnRowDataBound="GridView3_RowDataBound"           OnPageIndexChanging="GridView3_PageIndexChanging"    CssClass="Grid">
```

**[Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) 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;                 }}
```


---

Original Source: https://www.mindstick.com/forum/12856/how-to-give-gridview-cell-font-colour-according-to-condition

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
