Change ASP.NET GridView Cell Text Color Using C#

If you want to add forecolour to a specific cell of gridview you are on the right article, I will show you this through example code(zip of code sample is also available).

Here we go with the aspx file code,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        table
        {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }
        table th
        {
            background-color: #F7F7F7;
            color: #333;
            font-weight: bold;
        }
        table th, table td
        {
            padding: 5px;
            border: 1px solid #ccc;
        }
        table, table table td
        {
            border: 0px solid #ccc;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="100" />
          
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

Now the backend cs file(C#),

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
public partial class CS: System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[1] {
                    new DataColumn("Item")
                });
                dt.Rows.Add("I");
                dt.Rows.Add("Love");
                dt.Rows.Add("C# corner");
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
                GridView1.Rows[1].Cells[0].ForeColor = System.Drawing.Color.Blue;
                GridView1.Rows[2].Cells[0].ForeColor = System.Drawing.Color.Green;
            }
        }

That's all hope you like it.

Here is the output,

Change ASP.Net GridView cell Text Color using C#

Sharing is caring share with your coding buddies.