Wednesday, March 11, 2009

How to work around bugs in the SPGridView control

 

How to work around bugs in the SPGridView control

SharePoint has a little known web control called SPGridView.  This control can give you a SharePoint list-like view of data from any data source you can bind to (from a database or other sources).  It also has features to sort, filter and group the data in much the same way users can with SharePoint lists.  For more information on how to use this, Paul Robinson has two great posts: SPGridView and SPMenuField: Displaying custom data through SharePoint lists Part 1 and Part 2.  In addition, Robert Fridén has a good post on Filtering with SPGridView.  However, there are two bugs that can be annoying and cause confusion to users:

  • When both filtering and grouping are enabled, the first group title row sometimes disappears.
  • When both sorting and filtering are enabled, the sort indicator arrow image will sometimes appear in the wrong column.

Working around either of these bugs requires you to create your own web control based on SPGridView and overriding the CreateChildControls method.  Here is the full code listing for the solution to work around both bugs.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint.WebControls;

namespace AdventuresInConsulting
{
public class GridViewControl : SPGridView
{
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
// Due to a bug in SPDataView, we need to reset the private field previousGroupFieldValue so the
// first grouping row title will consistently show up
FieldInfo fieldInfo = typeof(SPGridView).GetField("previousGroupFieldValue",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fieldInfo != null)
fieldInfo.SetValue(this, null);

int returnValue = base.CreateChildControls(dataSource, dataBinding);

// Fix up a bug in the SPGridView where it is putting the sort arrow indicator on the wrong
// column if AllowFiltering is true
if (this.AllowFiltering && this.HeaderRow != null && !string.IsNullOrEmpty(this.SortExpression))
{
List<string> filterFields = new List<string>(this.FilterDataFields.Split(','));
if (this.AllowGrouping) filterFields.Insert(0, string.Empty);

for (int i = 0; i < this.HeaderRow.Cells.Count; i++)
{
DataControlFieldHeaderCell cell = this.HeaderRow.Cells[i] as DataControlFieldHeaderCell;
if (cell != null)
{
// Find the sort image control
Image sortImage = null;
foreach (Control c in cell.Controls)
{
sortImage = c as Image;
if (sortImage != null && sortImage.ImageUrl == this.SortDirectionImageUrl)
break;
}

// If this field is filterable, make sure the menu image is correct
if (i < filterFields.Count && filterFields[i].Trim() != string.Empty)
{
// If there is also an image control, remove it (we'll add it back in the right place)
if (sortImage != null) cell.Controls.Remove(sortImage);

// Find the menu control and add or remove the image in there as needed
foreach (Control c in cell.Controls)
{
if (c is Microsoft.SharePoint.WebControls.Menu)
{
Microsoft.SharePoint.WebControls.Menu menu = c as Microsoft.SharePoint.WebControls.Menu;
if (this.SortExpression.Equals(this.Columns[i].SortExpression, StringComparison.InvariantCultureIgnoreCase))
{
// Make sure it has the sort indicator
menu.RightImageUrl = this.SortDirectionImageUrl;
menu.ImageTextSpacing = new Unit("2px", CultureInfo.InvariantCulture);
}
else if (menu.RightImageUrl == this.SortDirectionImageUrl)
{
// Make sure it doesn't have the sort indicator
menu.RightImageUrl = null;
menu.ImageTextSpacing = Unit.Empty;
}
}
}
}
else
{
if (this.SortExpression.Equals(this.Columns[i].SortExpression, StringComparison.InvariantCultureIgnoreCase))
{
// Make sure there is a sort image
if (sortImage == null)
{
sortImage = new Image();
sortImage.ImageUrl = this.SortDirectionImageUrl;
sortImage.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
cell.Controls.Add(sortImage);
}
}
else if (sortImage != null)
{
// Remove the sort image if it exists
cell.Controls.Remove(sortImage);
}
}
}
}
}

return returnValue;
}
}
}


Fixing the group title row disappearance


Making it so that the first group title row doesn't disappear is fairly simple.  I noticed that this problem occurred if the filter or sort caused the first group in the new results to be the same as the last group from the previous results.  This led me to look at how SPGridView was checking for the start of a new group.  Sure enough, it was just checking to see if the value of the group field in the current row was different from the group field value the last time it detected a change.  It saves the last group value in a field called previousGroupFieldValue.  That value was being preserved between calls (presumably so that paging will work correctly).  The easy fix is to reset the last group value.  The problem was that the value was accessed through a private field.  Of course, thanks to .NET reflection, we won't let that stand in our way.  We just grab  the FieldInfo for previousGroupFieldValue and then forcibly set the value.



NOTE: you may need to modify this slightly if paging is enabled.  You probably only want to reset it if the page has not changed.



Fixing the sort indicator arrow image


Fixing the sort indicator arrow image is the more difficult of the two work arounds.  I had hoped to recreate the CreateChildControls method of SPGridView and just avoid the image from coming up in the wrong place.  However, it turns out that SPGridView calls its base class CreateChildControls method and there is no way in C# for a derived class to call its base's base class methods.  So, rather than trying to recreate a whole chain of CreateChildControls methods, I just let SPGridView put the arrows where it wanted to and then fixed them up later.



The code loops through all of the column headers and looks for the sort indicator image either as a standalone Image control, or as the RightImageUrl of the column menu's title.  We remove the image where it doesn't belong and add the image where it does belong.



Note on this bug: the issue arises because enabling grouping causes a hidden column to be added to the front of the grid and the code logic SPGridView uses to decide which column to put the filter indicator on is off by one.  You'd think it'd be off to the left, but it is actually off to the right because whoever wrote the code knew there was a hidden column, but they overcompensated for it (kind of feels like being in a car with a kid learning to drive with power steering for the first time).



Published Wednesday, March 11, 2009 6:11 PM by Valdon




Adventures in Consulting : How to work around bugs in the SPGridView control

No comments:

Blog Archive