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" "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 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.

Bookc
Admin

Hopefully the information we provide can be useful.

Press ESC to close