Saturday, May 9, 2009

SPGridview Grouping. Is it easy to apply??

Well, have you ever tried to create new sharepoint webpart with SPGrdiview and tried to enable grouping??

For the first time you call your grid it will work fine, but if you tried to use it in searching page(like you have textbox and submit button to bind the data to your grid based on entered text), it will come up with annoyed ASP.NET yellow error page saying:

" System.ArgumentNullException: Value canot be null "

the problem happened while rendering the rows again after the postback.
To solve this problem follow the steps below:
  1. Create new class @ your webpart project calling "SPGridViewGrouping".
  2. Let this class inherits from "SPGridview", your class should look like this:

    class SPGridViewGrouping : SPGridView
    {
    protected override void LoadControlState(object savedState)
    {
    base.LoadControlState(savedState);
    if (this.DataSource == null)
    {
    this.InvokeRequiresDataSource();
    }
    }
    public event EventHandler RequiresDataSource;
    private void InvokeRequiresDataSource()
    {
    //throw new NotImplementedException();
    EventHandler handler = this.RequiresDataSource;
    if (handler != null)
    {
    handler(this, new EventArgs());
    }
    }
    }
  3. At your webpart class create your grid object from "SPGridViewGrouping" NOT "SPGridview".
    SPGridViewGrouping oGrid = new SPGridViewGrouping();
  4. Add handler calling your menthod created @ SPGridViewGrouping
    oGrid.RequiresDataSource += new EventHandler(oGrid_RequiresDataSource);
  5. At this handler will look like:
    void oGrid_RequiresDataSource(object sender, EventArgs e)
    {
    //throw new NotImplementedException();
    oGrid.DataBind();
    }
  6. Enjoy your grouping.

12 comments:

Unknown said...

is the groupping working for you? i have fixed that cannot be null error but i still get weird functionality when sorting by group column, sometimes the first rows dont get the (+) for expading...is anyone else having this problem? or just me?

Henrik Andersson said...

Hi,

I disabled the viewstate for the SPGridView objcte and then this problem was resolved.

daspeac said...

I have heard about another way of fix .dbf files duplicate field name. Besides, you can visit my blogs at: http://daspeac.livejournal.com/ or http://daspeac.blogspot.com/ where I’m trying to share my experience with regard to data corruption issues.

Anonymous said...

Disable viewstate!!!!
it rocks!

Working

Michael Herman (Toronto) said...

This works without having to disable ViewState. Perfect. Thanks.

Tom Heylen said...

It seems on some environments you need to disable the viewstate. At least that is what I had to do, to make it work. Currently running in SharePoint 2010.

Shrikant Sawant said...

At the last Just write
gridView.EnableViewState = false;

you will see that error just foooooooooo....

Shrikant Sawant said...
This comment has been removed by the author.
David Campos said...
This comment has been removed by the author.
David Campos said...

I'm using (Sharepoint:SPGridView) in .apx layout page and i need to do so because i need to edit the template of spgridview and allow "in-line" row editing. I found many problems after start grouping data. Can anybody help me to find if it is possible to use the method described above but registering the control on aspx page, allowing me to edit the template?
Or according to my needs it is better to find another way to do that...?
Remember, i need allow "in-line" row editing, sort and group data.

GromRom said...

It is a GREAT POST!
Very BIG thank you!

Pradeepraj M said...

Hi guys,
Even after disabling viewstate, filtering a column and sorting a column makes clear filter to disable mode and only filtered items are visible in the grid. Please try to help me with this issue.