Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Showing Grouping Of Data in ASP.Net Grid View

Viewed:  3407 
Posted On:  09/02/2015 02:31:59 
How we can show group of data in Grid View? 

In this article I am going to show how we can show our data by making group in a Grid View in

Below is my Data Table Structure:


Image 1.

Data in My Data Table is:


Image 2. 

Now in Grid View I will show Data first by Country after it I will make group by State and in last City.

Below is my aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ShowingGroupingOfData.Default" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <title>Showing Grouping Of Data In ASP.NET Grid View</title>
    <form id="form1" runat="server">
            <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
                    <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-size:
                                20pt; color: red;">Showing Grouping Of Data In a ASP.NET Grid View
                    <td style="text-align: left;">
                        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" Width="100%">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <EditRowStyle BackColor="#999999" />
                            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />     
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#E9E7E2" />
                            <SortedAscendingHeaderStyle BackColor="#506C8C" />
                            <SortedDescendingCellStyle BackColor="#FFFDF8" />
                            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    </td>                </tr>
            </table>        </div>

Now my aspx.cs code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;using System.Configuration;
namespace ShowingGroupingOfData
    public partial class Default : System.Web.UI.Page
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
            if (!Page.IsPostBack)
        private void BindGrid()
            SqlConnection con = new SqlConnection();
            ds = new DataSet();
            con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";
            SqlCommand cmd = new SqlCommand("SELECT Country, State, City,Name, Designation  FROM Employee ORDER BY Country,State, City", con);
            da = new SqlDataAdapter(cmd);
            if (ds.Tables[0].Rows.Count > 0)
                GridView1.DataSource = ds.Tables[0];
                // First parameter is Row Collection
                // Second is Start Column
                // Third is total number of Columns to make group of Data.
                ShowingGroupingDataInGridView(GridView1.Rows, 0, 3);
        void ShowingGroupingDataInGridView(GridViewRowCollection gridViewRows, int startIndex, int totalColumns)
            if (totalColumns == 0) return;
            int i, count = 1;
            ArrayList lst = new ArrayList();
            var ctrl = gridViewRows[0].Cells[startIndex];
            for (i = 1; i < gridViewRows.Count; i++)
                TableCell nextTbCell = gridViewRows[i].Cells[startIndex];
                if (ctrl.Text == nextTbCell.Text)                
                    nextTbCell.Visible = false;
                    if (count > 1)
                        ctrl.RowSpan = count;
                        ShowingGroupingDataInGridView(new GridViewRowCollection(lst), startIndex + 1, totalColumns - 1);
                    count = 1;
                    ctrl = gridViewRows[i].Cells[startIndex];                   
            if (count > 1)
                ctrl.RowSpan = count;               
                ShowingGroupingDataInGridView(new GridViewRowCollection(lst), startIndex + 1, totalColumns - 1);
             count = 1;

Now run the application:

R 1.png

Image 3.

See here I am showing data first by Country after it by State and in last by City.

R 2.png

Image 4.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us