Wednesday, July 21, 2010

Gridview grouping

I've worked with SPGridView quite a bit lately and know that grouping is achievable, i thought that Gridview being the superclass of SPGridview, it should support grouping as well. I was wrong. My girlfriend had a requirement to group a set of data based on their type. A mock set of the data involved:


Product A - Type A
Product B - Type A
Product C - Type B
Product D -Type B
Product E - Type C
Product F - Type D


My first move was to take a look at the DAL (Data access layer) to see how the objects are related, in order for me to plan on grouping it. They are returned as generic dataset, so i had to create some simple POCO (Plain Old C# Objects), i prefer to work with strongly-typed grids :)


After retrieving the the data, i've modified the DAL to return an IList of the POCO instead of DS.


With the data retrieval and formatting done, all i have to do now is to bind it to a gridview and set the group by option. A little googling, i found a custom gridview which allow grouping:
ZNetControls

Read a little on the page and from the author's documentation, it seem simple enough.


  1. I created an ObjectDataSource that hooks up with the DAL GetAllBindableObjects() as the selectmethod. 
  2. Added the library (ZNetControls) into my VS and added the ZNetGridview into the page
  3. Added the grouping type: DataKeyNames="Category"
Ran the page and i get this:


Pretty easy to implement :)



1 comment: