locked
SPGridView grouping not working with sorting or paging RRS feed

  • Question

  • Anybody can help with the issue here? I have a SPGridView using ObjectDataSource with grouping, sorting and paging enabled. Once i clicked on the next button or the sort header, it's give the "An unexpected error has occurred." on screen. 

    Error in the 12 hive logs
    Application error when access /Web Pages/TestGridView.aspx, Error=Value cannot be null.  Parameter name: container
    at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
    at Microsoft.SharePoint.WebControls.SPGridView.OnRowCreated(GridViewRowEventArgs args)
    at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
    at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
    at Microsoft.SharePoint.WebControls.SPGridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
    at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
    at System.Web.UI.Control.EnsureChildControls()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    The source code in Test.ascx is:

    <%@ Control Language="C#" ClassName="Test1.Test" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Web.UI" %>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%@ Import Namespace="System.Net.Mime" %>
    <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
        Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
        Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="Microsoft" Namespace="Microsoft.Office.Server.Diagnostics"
        Assembly="Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Import Namespace="Microsoft.SharePoint.Administration" %>
    <%@ Import Namespace="Microsoft.Office.Server.Diagnostics" %>
    
    <script runat="server">
    
        SPGridView gridViewResult;
        SPGridViewPager gridViewPager;
        ObjectDataSource dataSource;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                dataSource = new ObjectDataSource();
                dataSource.ID = "dataSource";
                dataSource.ObjectCreating += new ObjectDataSourceObjectEventHandler(dataSource_ObjectCreating);
                dataSource.TypeName = this.GetType().AssemblyQualifiedName;
                dataSource.SelectMethod = "GetDataTable";
                Controls.Add(dataSource);
                
                gridViewResult = new SPGridView();
                gridViewResult.ID = "gridViewResult";
                gridViewResult.DataSourceID = dataSource.ID;
                gridViewResult.AutoGenerateColumns = false;
    
                //paging
                gridViewResult.AllowPaging = true;
                gridViewResult.PageSize = 3;
                // todo change back
                //gridViewResult.PagerSettings.PageButtonCount = 2; // by default is 10
    
                // sorting
                gridViewResult.AllowSorting = true;
                // gridViewResult.Sorting += new GridViewSortEventHandler(gridViewResult_Sorting);
    
                //filtering
                gridViewResult.AllowFiltering = true;
    
                // grouping
                gridViewResult.AllowGrouping = true;
                gridViewResult.AllowGroupCollapse = true;
                gridViewResult.GroupField = "Column C";
                gridViewResult.DisplayGroupFieldName = true;
    
                Controls.Add(gridViewResult);
                // gridViewResult.PagerTemplate = null;
    
                gridViewPager = new SPGridViewPager();
                gridViewPager.GridViewId = gridViewResult.ID;
                gridViewPager.ID = "gridViewPager";
                Controls.Add(gridViewPager);
    
                BindData();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message + " " + ex.StackTrace);
            }
        }
    
        void dataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = this;
        }
    
        private void BindData()
        {
            gridViewResult.Columns.Clear();
            gridViewResult.AllowSorting = true;
            SPBoundField boundField1 = new SPBoundField();
            boundField1.HeaderText = "Column A";
            boundField1.DataField = "Column A";
            boundField1.SortExpression = "Column A";
            gridViewResult.Columns.Add(boundField1);
    
            SPBoundField boundField2 = new SPBoundField();
            boundField2.HeaderText = "Column B";
            boundField2.DataField = "Column B";
            boundField2.SortExpression = "Column B";
            gridViewResult.Columns.Add(boundField2);
    
            SPBoundField boundField3 = new SPBoundField();
            boundField3.HeaderText = "Column C";
            boundField3.DataField = "Column C";
            boundField3.SortExpression = "Column C";
            gridViewResult.Columns.Add(boundField3);
    
            SPBoundField boundField4 = new SPBoundField();
            boundField4.HeaderText = "Column D";
            boundField4.DataField = "Column D";
            boundField4.SortExpression = "Column D";
            gridViewResult.Columns.Add(boundField4);
    
            gridViewResult.DataBind();
    
        }
    
        public DataTable GetDataTable()
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("Column A");
            tb.Columns.Add("Column B");
            tb.Columns.Add("Column C");
            tb.Columns.Add("Column D");
    
            for (int i = 0; i < 20; i++)
            {
                DataRow row = tb.NewRow();
                row["Column A"] = "A" + i;
                row["Column B"] = "B" + i;
                if (i<5)
                    row["Column C"] = "C0";
                else
                row["Column C"] = "C" + i;
                row["Column D"] = "D" + i;
                tb.Rows.Add(row);
            }
    
            return tb;
        }
    
    </script>
    
    <asp:Literal ID="LiteralMsg" runat="server"></asp:Literal>


    Thanks in advanced. 

    • Edited by jingzo Tuesday, July 7, 2009 7:09 AM
    Tuesday, July 7, 2009 7:06 AM

Answers

  • After several tests, I found that if you disable the ViewState of the SPGridView the storing or paging will work.

     

                gridViewResult = new SPGridView();

                gridViewResult.ID = "gridViewResult";

                gridViewResult.DataSourceID = dataSource.ID;

                gridViewResult.AutoGenerateColumns = false;

     

                gridViewResult.EnableViewState = false;

     

                //paging

                gridViewResult.AllowPaging = true;

                gridViewResult.PageSize = 3;

     

    Cheers.

    Wednesday, July 8, 2009 4:15 AM

All replies

  • After several tests, I found that if you disable the ViewState of the SPGridView the storing or paging will work.

     

                gridViewResult = new SPGridView();

                gridViewResult.ID = "gridViewResult";

                gridViewResult.DataSourceID = dataSource.ID;

                gridViewResult.AutoGenerateColumns = false;

     

                gridViewResult.EnableViewState = false;

     

                //paging

                gridViewResult.AllowPaging = true;

                gridViewResult.PageSize = 3;

     

    Cheers.

    Wednesday, July 8, 2009 4:15 AM
  • Thanks Saved my time. That was very helpful.
    Vignesh Sugumar
    Monday, October 19, 2009 10:41 PM
  • Thank you .Chengyi Wu



    Friday, March 5, 2010 6:29 AM
  • thanks a lot, I have spent so so so much time in this error....

     

    Wednesday, June 16, 2010 1:21 PM
  • Saved a lot of my time. Thanks a million.
    Thursday, July 8, 2010 6:48 PM
  • Tahnks for the tip. could you describe why it is not working with ViewState Enabled state?

    Also where we could find some documentation about SharePoint 2010 controls? Existing documentation is very poor.

     

    Thanks in Advance

    Saturday, October 9, 2010 11:47 AM