Thursday 17 May 2012

Problem Sorting With Multiple SPGridViews

I have experienced a funny behavior by having two SPGridviews along with ObjectDataSource on the same page. Both the SPGirdViews are enabled with sorting, filtering and pagination. Filtering and Pagination worked perfectly without any issues. If it comes to sorting point of view, whenever you try to sort the lower SPGridView it gets applied to upper SPGridView.

The Hack for this issue:
              protected override void OnPreRender(EventArgs e)
        {           
            if (SPGridView1.HeaderRow != null)
            {
                foreach (WebControl control in this.SPGridView1.HeaderRow.Controls)
                {
                    UpdateTemplateClientID(control, SPGridView1.ClientID);
                }
            }
            if (SPGridView2.HeaderRow != null)
            {
                foreach (WebControl control in this.SPGridView2.HeaderRow.Controls)
                {
                    UpdateTemplateClientID(control, SPGridView2.ClientID);
                }
            }
            base.OnPreRender(e);
        }

        private void UpdateTemplateClientID(Control control, string clientID)
        {
            if (control is Microsoft.SharePoint.WebControls.Menu)
            {
                Microsoft.SharePoint.WebControls.Menu menuControl = control as Microsoft.SharePoint.WebControls.Menu;
                string jsFunctionCall = menuControl.ClientOnClickPreMenuOpen;
                menuControl.ClientOnClickPreMenuOpen = jsFunctionCall.Replace("%TEMPLATECLIENTID%", clientID + "_SPGridViewFilterMenuTemplate");
            }
            else if (control.HasControls())
            {
                foreach (WebControl c in control.Controls)
                {
                    UpdateTemplateClientID(c, clientID);
                }
            }
        }

2 comments:

  1. Hi,

    I'm facing issue with filtering on multiple grids. Can you please help me on this? clearing of a filter in one spgrid is clearing another spgird filter also..

    ReplyDelete
  2. also faing issue with getting filter menu sometimes

    ReplyDelete