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

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