Gridview Grouping in C#.Net


In this post, I explain how to group data in gridview using C#.NET. Sometimes we need to show group data in our ASP.NET Application like show List of State group by Country. Here I have done this using C#.NET. Steps are given below:-

For MVC Developer visit:  Grouping Gridview in MVC 4


I used followings:
  • .Net framework 4.0
  • Entity Framework
  • SQL Server 2008

Steps :

Just follow the steps and get result easily.

Step - 1 : Create New Project

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create two tables and insert data for show grouped data in Gridview

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have created two table 1. CountryMaster 2. StateMaster.
Create table and insert data for Show in grouped gridview.

Step-4: Add Needed files( 2 js, 1 CSS and some images).

These files are required to design our page and to give better looks.
I have added this files in the Scripts folder.
Right Click on the Scripts folder > Add > Existing Item > Select Files > Add.
Here I have added some images in this location --> Root > Scripts > Images

Step-5: Design Web Page (Default.aspx).

Just Write the following code to your page.

Inside Head Content

<link href="Scripts/jquery-ui.css" rel="stylesheet" />
    <script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui.js" type="text/javascript"></script>
        .ui-icon {display: inline-block; text-indent: -99999px; overflow: hidden; background-repeat: no-repeat; }
    <script language="javascript">
        $(function () {
                collapsible: true,
                autoHeight: false

 Inside Body Content

        Gridview Grouping Example
            <asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="ItemPH" OnItemDataBound="ListView1_ItemDataBound">
                    <div id="accordion">
                        <asp:PlaceHolder ID="ItemPH" runat="server" />
                    <h3>Country : <%#Eval("CountryName") %></h3>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                            <asp:TemplateField HeaderText="State ID">
                                    <%#Eval("StateID") %>
                            <asp:TemplateField HeaderText="State Name">
                                    <%#Eval("StateName") %>

Step-6: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-7: Add Code For Populate Country Data.

Write the followings code in your page load event for fetch Country Data from Database.

      protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)

And Here is the function code

      private void PopulateCountry()
            using (MyDatabaseEntities dc = new MyDatabaseEntities())
                var v = dc.CountryMasters.ToList();
                ListView1.DataSource = v;

Step-8: Add Code For Populate State Data.

Write the followings code in your ListView ItemDataBound event for fetch State Data from Database.

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
            if (e.Item.ItemType == ListViewItemType.DataItem)
                GridView grd1 = (GridView)e.Item.FindControl("GridView1");
                CountryMaster cm = e.Item.DataItem as CountryMaster;
                if (cm != null)
                    if (grd1 != null)
                        using (MyDatabaseEntities dc = new MyDatabaseEntities())
                            var v = dc.StateMasters.Where(a => a.CountryID.Equals(cm.CountryID)).ToList();
                            grd1.DataSource = v;

STEP-9: Run Application

Run Application and Get result in your browser.

Thank You

Download Source Code...

Related Post

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.