Showing Grouping of Data in ASP.Net GridView

This article shows how to show data using horizontal groups in a GridView in ASP.Net.

The following is my Data Table structure:



The data in my Data Table is:



Now in a Grid View I will show the data first by country then in groups of states and last by city.

The following is my aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ShowingGroupingOfData.Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>Showing Grouping Of Data In ASP.NET Grid View</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <div>  
  12.             <table style="border: solid 15px blue; width: 100%; vertical-align: central;">  
  13.                 <tr>  
  14.                     <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  
  15.                     </td>  
  16.                 </tr>  
  17.                 <tr>  
  18.                     <td style="text-align: left;">  
  19.                         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" Width="100%">  
  20.                             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  21.                             <EditRowStyle BackColor="#999999" />  
  22.                             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  23.                             <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  24.                             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  25.                             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  26.                             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  27.                             <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  28.                             <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  29.                             <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  30.                             <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  31.                         </asp:GridView>  
  32.                     </td>  
  33.                 </tr>  
  34.             </table>  
  35.         </div>  
  36.     </form>  
  37. </body>  
  38. </html>  
Now my aspx.cs code:
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.IO;  
  9. using System.Data;  
  10. using System.Data.SqlClient;  
  11. using System.Configuration;  
  12.   
  13. namespace ShowingGroupingOfData  
  14. {  
  15.     public partial class Default : System.Web.UI.Page  
  16.     {  
  17.         SqlDataAdapter da;  
  18.         DataSet ds = new DataSet();  
  19.         DataTable dt = new DataTable();  
  20.   
  21.         protected void Page_Load(object sender, EventArgs e)  
  22.         {  
  23.             if (!Page.IsPostBack)  
  24.             {  
  25.                 BindGrid();  
  26.   
  27.             }  
  28.         }  
  29.   
  30.         private void BindGrid()  
  31.         {  
  32.             SqlConnection con = new SqlConnection();  
  33.             ds = new DataSet();  
  34.             con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";  
  35.             SqlCommand cmd = new SqlCommand("SELECT Country, State, City,Name, Designation  FROM Employee ORDER BY Country,State, City", con);  
  36.   
  37.             da = new SqlDataAdapter(cmd);  
  38.             da.Fill(ds);  
  39.             con.Open();  
  40.             cmd.ExecuteNonQuery();  
  41.             con.Close();  
  42.   
  43.             if (ds.Tables[0].Rows.Count > 0)  
  44.             {  
  45.                 GridView1.DataSource = ds.Tables[0];  
  46.                 GridView1.DataBind();  
  47.   
  48.                 // First parameter is Row Collection  
  49.                 // Second is Start Column  
  50.                 // Third is total number of Columns to make group of Data.  
  51.                 ShowingGroupingDataInGridView(GridView1.Rows, 0, 3);  
  52.             }  
  53.         }  
  54.   
  55.         void ShowingGroupingDataInGridView(GridViewRowCollection gridViewRows, int startIndex, int totalColumns)  
  56.         {  
  57.             if (totalColumns == 0) return;  
  58.             int i, count = 1;  
  59.             ArrayList lst = new ArrayList();  
  60.             lst.Add(gridViewRows[0]);  
  61.             var ctrl = gridViewRows[0].Cells[startIndex];  
  62.             for (i = 1; i < gridViewRows.Count; i++)  
  63.             {  
  64.                 TableCell nextTbCell = gridViewRows[i].Cells[startIndex];  
  65.                 if (ctrl.Text == nextTbCell.Text)  
  66.                 {  
  67.                     count++;  
  68.                     nextTbCell.Visible = false;  
  69.                     lst.Add(gridViewRows[i]);   
  70.                 }  
  71.                 else  
  72.                 {  
  73.                     if (count > 1)  
  74.                     {  
  75.                         ctrl.RowSpan = count;   
  76.                         ShowingGroupingDataInGridView(new GridViewRowCollection(lst), startIndex + 1, totalColumns - 1);  
  77.                     }  
  78.                     count = 1;  
  79.                     lst.Clear();                      
  80.                     ctrl = gridViewRows[i].Cells[startIndex];  
  81.                     lst.Add(gridViewRows[i]);  
  82.                 }  
  83.             }  
  84.             if (count > 1)  
  85.             {  
  86.                 ctrl.RowSpan = count;                  
  87.                 ShowingGroupingDataInGridView(new GridViewRowCollection(lst), startIndex + 1, totalColumns - 1);  
  88.             }  
  89.             count = 1;  
  90.             lst.Clear();  
  91.         }  
  92.     }  
  93. }  
Now run the application. 

See here I am showing the data grouped horizontally first by country then by state and last by city.