IntroductionIn 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
PrerequisiteI used followings:
- .Net framework 4.0
- Entity Framework
- SQL Server 2008
Steps :Just follow the steps and get result easily.
Step - 1 : Create New ProjectGo 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 GridviewOpen 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
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.
And Here is the function code
Step-8: Add Code For Populate State Data.Write the followings code in your ListView ItemDataBound event for fetch State Data from Database.
STEP-9: Run ApplicationRun Application and Get result in your browser.
Download Source Code...