Valdon11 Mar 2009 8:11 PM
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).
发表评论
-
Creating Hierarchical Menus with a CustomAction in SharePoint
2011-11-17 14:11 884原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 921原文链接 http://johnholliday.net/ ... -
Filtering with SPGridView
2011-10-12 15:23 751原文链接 http://vspug.com/bobsbon ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1046原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1268原文链接 http://blogs.msdn.com/b/ ... -
How to add a custom action to a SharePoint list actions menu for a specific list
2011-10-11 10:56 926原文链接 http://www.nearinfinity. ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 901原文链接 http://www.csharpest.net ... -
Adding CheckBoxes in SharePoint GridView (SPGridView)
2011-10-10 17:01 920原文链接 http://www.c-sharpcorner ... -
SharePoint的列级安全性
2011-10-10 11:25 1131原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1696原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1382原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1241原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 851原文链接 http://msdn.microsoft ...
相关推荐
What bugs in the cloud?A Study of 3000+ Issues in Cloud Systems
“What bugs live in the cloud? A study of 3000+ issues in cloud system” 论文ppt
* to work around bugs in some JS interpreters. * 需要加密的文字或者数字作为必要参数传入: hexMD5.hexMD5(参数); (Text or Numbers that need to be encrypted are passed in as necessary parameters: ...
XL Report is a set of Delphi-components for report generation in Microsoft Excel by means ... We used variables of Variant data type only once or twice to work around some bugs in the Excel Type Library.
Everybody claims that it is easy to find and fix bugs in programs written under Linux. Unfortunately it is very hard to find documents explaining how to do that. In this article you will learn how to ...
discovRE: Efficient Cross-Architecture Identification of Bugs in Binary Code是发表在NDSS'16会议上的论文。本文在SP'15的基础上提出了在效率和效果上都有提高的跨架构二进制代码漏洞检测方案。 Abstract & ...
· Perl "gotchas"-how to understand them, work around them, and avoid them · "Antibugging"-the authors' rules of thumb on how to code defensively · The Perl debugger -the authors' guide to using ...
This is believed to be a bug in the Windows clipboard and a work around hasn't been found yet. * Asynchronous targets appears to be broken in the current release. * When TDropFileTarget....
66 communicAtions of the Acm | FeBrUAry 2010 | vOl. 53 | nO. 2contributed articlesIn 2 002, COVErITY commercialized3 a research static bug-finding tool.6,9 Not surprisingly, as academics, our view ...
Find the tools that make the code editor pleasant to work with, even in long coding sessions. Discover the right way to find and fix bugs when you have lots of code that’s not always playing nicely ...
- Testing - learn how automated testing can make your code more reliable, how to catch bugs before they impact your work, and how to edit code with confidence. - Performance - learn how to make your ...
Zoom to extents functionality is now working, which will allow you to create a drawing and then adjust the view so that the drawing opens in autocad zoomed to its current extents. The Layer ...
and moved the RecyclerView chapter to be earlier in the book • Added some material on the Chrome OS emulator • Moved the material on SlidingPaneLayout to the Widget Catalog • Made minor ...
It is imperative to determine if bugs do exist and then be able to metric how quickly they can be identified, the cost they incur, and how many remain in the product when it is released. With this ...
Testing - learn how automated testing can make your code more reliable, how to catch bugs before they impact your work, and how to edit code with confidence. Performance - learn how to make your code ...
Chapter 3—The Role of Access Control in Your Environment Configuration Problems Program Bugs What Is Access Control? How Are Access Control Decisions Made? Access Control Lists Who Are You? ...
and destructorsDiscover how the concept of inheritance is the key to effective C++ programmingWork with assignment operators, stream I/O, and other more advanced concepts, once you’ve grasped the ...
During the Core elections in 2002, Mark Murray stated “I am opposed to a long rule-book, as that satisfies lawyer-tendencies, and is counter to the technocentricity that the project so badly needs.”...
To work around this is a major hack.By adding the Menus unit to the project the Forms unit gets pulled in along with it bloating the DLL. With EasyNSE a fully functional context menu DLL can be as ...
Bugs as deviant behavior__a general approach to inferring errors in systems code