How To Change ASP NET GridView Cell Text Color Using C sharp

BOOKC, this time share about How To Change ASP NET GridView Cell Text Color Using C sharp

If you want to add a foreground color to a specific cell of the gridview you are in the right article, I will demonstrate this via sample code (sample zip code 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" "">
<html xmlns="">
<head runat="server">
    <style type="text/css">
            font-family: Arial;
            font-size: 10pt;
            border: 1px solid 
            border-collapse: collapse;
        table th
            font-weight: bold;
        table th, table td
            padding: 5px;
            border: 1px solid 
        table, table table td
            border: 0px solid 
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="100" />


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("C# corner");
                GridView1.DataSource = dt;
                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 the article about How To Change ASP.NET GridView Cell Text Color Using C# hope you like it. And don't forget to share it on your social media.


Hopefully the information we provide can be useful.

Press ESC to close