Hi all,

I have a table with 6 rows and 5 columns. First row is the header, I want to change color for the values in the 5th column, i.e. if the value is >0 then the font color is red, if value is <0 then its green.

I have a code, buts its not working on my page.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
      GridView1.DataBind();
        int XZ = Convert.ToInt32(Convert.ToInt32(GridView1.Rows[1].Cells[4].Text));

        if (XZ > 0)
        {
            GridView1.Rows[1].Cells[4].ForeColor = Color.LimeGreen;
                 }
        else if (XZ < 0)
        {
            GridView1.Rows[1].Cells[4].ForeColor = Color.Red;
                }
        else if (XZ == 0)
        {
             GridView1.Rows[1].Cells[4].ForeColor = Color.Black;
                   }
    }

and this is my aspx page code:

<asp:GridView ID="GridView1" runat="server" BackColor="LightGray" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" ForeColor="Black" Height="139px" Width="210px" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >

Please guide me to fix this issue, as I am a net admin, and our developer is on vacation. If urgent to fix it as we already are hosting these table on our site.

Thank you.

I am confusing in my question, please hint me, if there is any further details required,

Thank you.

The code need to be written in RowDataBound event:
I have taken SupplierID column for demo,change according to your need.Columns starts with 0 index.
Try this:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           DataRow pr =   ((DataRowView)e.Row.DataItem).Row;
           int id = Convert.ToInt32(pr["SupplierID"]);
            if(id==1)
                e.Row.Cells[2].BorderColor = System.Drawing.Color.Red;
            if(id < 1)
                e.Row.Cells[2].BorderColor = System.Drawing.Color.Blue;
            if(id==0)
                e.Row.Cells[2].BorderColor = System.Drawing.Color.Violet;      

        }

    }

Edited 3 Years Ago by Dani: Formatting fixed

Thank you Ravichandra for your reply, but there are some queries:

I have changes SupplierID to the BoundField DataField="col5" (which is the column name or ID, i suppose) which part of the aspx page code as

<asp:BoundField DataField="col5" DataFormatString="{0:F3}" HeaderText="col5" SortExpression="col5" HtmlEncode="False"

, there is no changes.

Also what is e.Row.Cells[2]? which cell its referring to.

Also when changing BorderColor to FontColor, its giving error as below:
'System.Web.UI.WebControls.TableCell' does not contain a definition for 'ForeColor' as we need the font to change color.

I have done no changes to the aspx page code.

Thank you again.

I Tested and its working.
Please go through and it works.
Good luck

ASPX

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Style="z-index: 100;
            left: 211px; position: absolute; top: 213px" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:BoundField DataField="SupplierID">
                 </asp:BoundField>
                 <asp:BoundField DataField="ProductName"></asp:BoundField>
                <asp:BoundField DataField="ProductID"></asp:BoundField>
            </Columns>
        </asp:GridView>

ASPX.CS

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void BindGrid()
    {
        SqlConnection con = new SqlConnection(conString);
        SqlCommand command = new SqlCommand("select * from products", con);

        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        adapter.Fill(ds, "Products");
        GridView1.DataSource = ds.Tables["Products"];
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRow pr = ((DataRowView)e.Row.DataItem).Row;
            int id = Convert.ToInt32(pr["ProductID"]);
            if (id == 1)
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
            if (id ==2)
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Blue;
            if (id == 3)
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Violet;

        }

    }

    public string conString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
        }
    }
}

In <asp:BoundField DataField="ProductID"></asp:BoundField> DataField (i.e ProductID) is the column field of the table which you are going to bind to the GridView.

e.Row.Cells[2] refers to the third column in the GridView.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("style", "cursor:help;");
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
        { 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");
                e.Row.BackColor = Color.FromName("#E56E94");                
           }           
        }
        else
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");
                e.Row.BackColor = Color.FromName("gray");                
            }
            //e.Row.Cells[0].BackColor = Color.FromName("gray");
            //e.Row.Cells[1].BackColor = Color.FromName("gray");
            //e.Row.Cells[2].BackColor = Color.FromName("gray");
            //e.Row.Cells[3].BackColor = Color.FromName("gray");
            //e.Row.Cells[4].BackColor = Color.FromName("gray");
            //e.Row.BorderWidth = 2;
            //e.Row.BorderColor = Color.FromName("#43C6DB");
        }
    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

public class connection
{
    SqlConnection con = new SqlConnection();
    DataTable dataTable;
    SqlCommand cmd;
    SqlDataAdapter adp;
    public connection()
    {
        string constring = ConfigurationManager.ConnectionStrings["constrwc"].ToString();
        con.ConnectionString = constring;
    }  
    public void Insert_data(string query,DataSet ds)
    {
        con.Open();
        SqlCommand cmd=new SqlCommand();
        cmd.Connection = con;        
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = query;
        SqlDataAdapter adp=new SqlDataAdapter(cmd);
        adp.SelectCommand=cmd;
        adp.Fill(ds);
    }
    public DataTable sqlSelect(string query)
    {
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = query;
        cmd.CommandType = CommandType.Text;
        adp = new SqlDataAdapter();
        adp.SelectCommand = cmd;
        dataTable = new DataTable();
        adp.Fill(dataTable);
        return dataTable;

    }
    public void sqlInsUpdDel(string query)
    {
        con.Open();
        cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = query;
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();        
    }    
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

This question has already been answered. Start a new discussion instead.