Home > Articles > ASP.NET Articles > How to change alternative color in asp.net gridview rows and columns?

How to change alternative color in asp.net gridview rows and columns?

by Thamil   on Aug 10, 2013   Category: ASP.NET  | Level: Intermediate  |  Views: 23591    |  Points: 100   
Like this article? Bookmark and Share:
In this article, I will show you how to set alternative color for gridview rows and columns in asp.net.

Introduction

Sometime we may have a cosmetic requirement in gridview control to set alternative row and column color in asp.net applications while displaying the data to the user. It’s easy and simple way to set alternative color in gridview control, follow the below steps and code snippets for this.

Create Asp.Net project


Create an asp.net project and format the aspx page as like below. I have added two gridview controls to show this sample. One for gridview alternative row color changes and other one is alternative column color change.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GVFormat._Default" %>
<!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>Gridview formatting sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <u><b>How to change alternative color for Gridview rows and columns?</b></u>
    </div>
    <br />
    <div>
        <div>
            1. Alternative color for Gridview rows
        </div>
        <br />
        <asp:GridView ID="GridVwRowcolorchange" runat="server" AutoGenerateColumns="False"
            Font-Names="Verdana" PageSize="5" Width="75%" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px">
            <AlternatingRowStyle BackColor="#BFE4FF" />
            <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
            <HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
                BorderStyle="Solid" BorderWidth="1px" />
            <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px" />
            <Columns>
                <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" />
                <asp:BoundField DataField="Emp_id" HeaderText="Employee ID" />
                <asp:BoundField DataField="Emp_job" HeaderText="Job title" />
                <asp:BoundField DataField="Emp_Dep" HeaderText="Department" />
            </Columns>
        </asp:GridView>
    </div>
    <br />
    <div>
        <div>
            2. Alternative color for Gridview colunms
        </div>
        <br />
        <asp:GridView ID="GridVwColcolorchange" runat="server" AutoGenerateColumns="False"
            Font-Names="Verdana" PageSize="5" Width="75%" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px">
            <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
            <HeaderStyle Height="30px" Font-Size="15px" BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px" />
            <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px" />
            <Columns>
                <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" HeaderStyle-BackColor="#C4C4C4"
                    ItemStyle-BackColor="#E2E2E2" />
                <asp:BoundField DataField="Emp_id" HeaderText="Employee ID" HeaderStyle-BackColor="#FFB07F"
                    ItemStyle-BackColor="#FFCAA8" />
                <asp:BoundField DataField="Emp_job" HeaderText="Job title" HeaderStyle-BackColor="#C4C4C4"
                    ItemStyle-BackColor="#E2E2E2" />
                <asp:BoundField DataField="Emp_Dep" HeaderText="Department" HeaderStyle-BackColor="#FFB07F"
                    ItemStyle-BackColor="#FFCAA8" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


 

Alternative Row color


The above first gridview name would be GridVwRowcolorchange, in this I have added  AlternatingRowStyle property which is used to change the alternative row color in the gridview control.

<AlternatingRowStyle BackColor="#BFE4FF" />

 

Alternative coloumn color


Above second gridview name would be GridVwColcolorchange, in this gridview i have added HeaderStyle-BackColor and ItemStyle-BackColor properties in boundfield. The HeaderStyle-BackColor property will be used to change specific header background color and ItemStyle- BackColor would be used to change specific column backgrounf color.

<asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" HeaderStyle-BackColor="#C4C4C4"
ItemStyle-BackColor="#E2E2E2" />
<asp:BoundField DataField="Emp_id" HeaderText="Employee ID" HeaderStyle-BackColor="#FFB07F"
ItemStyle-BackColor="#FFCAA8" />

Here alternatively I have changed specific header and associated column color. This is the one of way to set alternative header and column color in the gridview control.

Code behind part

 using System;
using System.Data;
namespace GVFormat
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetEmpDept();
            }
        }
        private void GetEmpDept()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Emp_Name");
            dt.Columns.Add("Emp_id");
            dt.Columns.Add("Emp_job");
            dt.Columns.Add("Emp_Dep");
            DataRow rw1 = dt.NewRow();
            rw1["Emp_Name"] = "Narain Sidharth";
            rw1["Emp_id"] = "2020";
            rw1["Emp_job"] = "SOftware Engineer";
            rw1["Emp_Dep"] = "IT";
            dt.Rows.Add(rw1);
            DataRow rw2 = dt.NewRow();
            rw2["Emp_Name"] = "Prakalathan";
            rw2["Emp_id"] = "1978";
            rw2["Emp_job"] = "System Engineer";
            rw2["Emp_Dep"] = "IT";
            dt.Rows.Add(rw2);
            DataRow rw3 = dt.NewRow();
            rw3["Emp_Name"] = "Mathu kumar";
            rw3["Emp_id"] = "2700";
            rw3["Emp_job"] = "Support Enginner";
            rw3["Emp_Dep"] = "IT";
            dt.Rows.Add(rw3);
            DataRow rw4 = dt.NewRow();
            rw4["Emp_Name"] = "Arvind";
            rw4["Emp_id"] = "4678";
            rw4["Emp_job"] = "Sr Software Engineer";
            rw4["Emp_Dep"] = "IT";
            dt.Rows.Add(rw4);
            DataRow rw5 = dt.NewRow();
            rw5["Emp_Name"] = "Raja ram";
            rw5["Emp_id"] = "2300";
            rw5["Emp_job"] = "Test Engineer";
            rw5["Emp_Dep"] = "IT";
            dt.Rows.Add(rw5);
            GridVwRowcolorchange.DataSource = dt;
            GridVwRowcolorchange.DataBind();
            GridVwColcolorchange.DataSource = dt;
            GridVwColcolorchange.DataBind();
        }
    }
}

Above code behind part would be used to bind the few sample records with gridview control

Thanks for reading this article and I hope you enjoyed this. Please post your feedback and suggestion about this article.



Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts