One of the most exciting new web controls that come with SharePoint 2007 is the SPGridView control. With it, you can present data in a grid view that closely resembles the view that SharePoint uses to display data from lists. You can sort and group rows, add drop-down menus to cells and filter rows using column header drop-down menus.
There is, however, a slight drawback with it; there is almost no official documentation on how to use it. But there is lots of information post on it around the 'net. If you're new to the SPGridView, I suggest you head over to Powlo's blog that has a great introduction of how to use the basic features of the control.
I recently chose to use this control in a scenario where filtering using the UI was required. I had heard about the SPGridView and knew it had the filtering capabilities I needed, but I had a very hard time figuring out how to use it. But after much (unsuccessful) Googling and a lot of trial-and-error on my own, I managed to get it right and thought it would be nice to share this information with others facing the same challenges I did.
The first hurdle I ran into was that filtering does not work if you bind the SPGridView using the DataSource property; you have to use the DataSourceID property to perform the binding. For this example, I wrote a small class that creates a DataTable object with some hardcoded data:
public static class ExampleObjectDataSource { public static DataTable GetDataTable() { DataTable tblData = new DataTable(); tblData.Columns.Add("Title"); tblData.Columns.Add("Author"); tblData.Columns.Add("Rating"); tblData.Rows.Add("Obsession", "Robards, Karen", 2); tblData.Rows.Add("Vanished", "Robards, Karen", 3); tblData.Rows.Add("Magician: Apprentice", "Feist, Raymong E.", 4); tblData.Rows.Add("Magician: Master", "Feist, Raymong E.", 5); tblData.Rows.Add("Silverthorn", "Feist, Raymong E.", 4); tblData.Rows.Add("Lord Foul's Bane", "Donaldson, Stephen R.", 3); tblData.Rows.Add("The Illearth War", "Donaldson, Stephen R.", 4); tblData.AcceptChanges(); return tblData; } }
Then simply instantiate an ObjectDataSource object that points to the example data class in the CreateChildControl() method of the web part:
ObjectDataSource odsDataSource = new ObjectDataSource( "Strand.Demos.FilteredGridExample.ExampleObjectDataSource, Strand.Demos.FilteredGridExample, Culture=neutral, Version=1.0.0.0, PublicKeyToken=9f4da00116c38ec5", "GetDataTable"); odsDataSource.ID = "ExampleSource"; Controls.Add(odsDataSource);
Of course, we're going to need some columns to display the data. For good measure, I want to implement sorting as well, so let's set the SortExpression properties of the fields too:
sgvGrid = new SPGridView(); sgvGrid.ID = "ExampleGrid"; sgvGrid.AutoGenerateColumns = false; BoundField col = new BoundField(); col.DataField = "Title"; col.SortExpression = "Title"; col.HeaderText = "Title"; sgvGrid.Columns.Add(col); col = new BoundField(); col.DataField = "Author"; col.SortExpression = "Author"; col.HeaderText = "Author"; sgvGrid.Columns.Add(col); col = new BoundField(); col.DataField = "Rating"; col.SortExpression = "Rating"; col.HeaderText = "Rating"; sgvGrid.Columns.Add(col); sgvGrid.AllowSorting = true; Next, let's enable the filtering! sgvGrid.AllowFiltering = true;
Now, we need to tell the SPGridView which columns we want to enable filtering on, and which field each column should use in the filter expression by setting the FilterDataFields property. This is a comma-separated string containing which field each respective column in the grid should filter by. We want to be able to filter by Author or Rating, but not by Title, so leave the first field empty:
sgvGrid.FilterDataFields = ",Author,Rating";
We also need to tell the grid which property to set on the data source control when filtering. In our case, it is the FilterExpression property of the ObjectDataSource:
sgvGrid.FilteredDataSourcePropertyName = "FilterExpression";
The SPGridView control will use string.Format() to generate the string it will set the FilterExpression to, so we must specify the format string it will use. The first parameter is the value to filter on, and the second parameter is the name of the field being filtered.
sgvGrid.FilteredDataSourcePropertyFormat = "{1} LIKE '{0}'";
And lest we forget, let's set the DataSourceID property of the grid and add the grid to the Controls collection:
sgvGrid.DataSourceID = "ExampleSource"; Controls.Add(sgvGrid);
Do the databinding in the Render method:
protected override void Render(HtmlTextWriter writer) { sgvGrid.DataBind(); base.Render(writer); }
Now you'll have filtering implemented and you can compile, deploy and filter the grid to your heart's content!
(Please excuse the Swedish UI!)
If you've come this far and played around with the filtering and sorting for awhile, you've probably noticed that there's something that's not working quite as you'd expect it; If you filter by one column and then sort by another, your filter will disappear! This is of course highly unsatisfactory and needs to be dealt with!
I think my solution to this is a bit on the ugly side, so if anyone out there happens to know the "right" way of doing this, I'd appreciate it if you let me know!
What I did was to first save the FilterExpression in the ViewState in the OnPreRender method:
protected override void OnPreRender(EventArgs e) { ViewState["FilterExpression"] = odsDataSource.FilterExpression; base.OnPreRender(e); }
Then I inserted code to set the FilterExpression to the previously saved expression in the ViewState. Since I never want the options in the filter menu drop-down to be filtered, I check the query string and then only set the FilterExpression if the current page request is not callback from the drop down menu. This code should be inserted just before adding the odsDataSource object to the Controls collection:
HttpRequest req = HttpContext.Current.Request; if (req.Form["__CALLBACKID"] == null || req.Form["__CALLBACKPARAM"] == null || !req.Form["__CALLBACKID"].EndsWith("ExampleGrid")) { if (ViewState["FilterExpression"] != null) odsDataSource.FilterExpression = (string)ViewState["FilterExpression"]; }
And that's it!
发表评论
-
Creating Hierarchical Menus with a CustomAction in SharePoint
2011-11-17 14:11 920原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 1010原文链接 http://johnholliday.net/ ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1096原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1295原文链接 http://blogs.msdn.com/b/ ... -
How to work around bugs in the SPGridView control
2011-10-11 16:10 830原文链接 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 957原文链接 http://www.nearinfinity. ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 950原文链接 http://www.csharpest.net ... -
Adding CheckBoxes in SharePoint GridView (SPGridView)
2011-10-10 17:01 970原文链接 http://www.c-sharpcorner ... -
SharePoint的列级安全性
2011-10-10 11:25 1161原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1736原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1430原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1298原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 899原文链接 http://msdn.microsoft ...
相关推荐
动态时空协同过滤是一种针对推荐系统和客户偏好模型设计时考虑时间动态变化的技术。在推荐系统中,用户对产品的偏好会随着时间而发生漂移,产品的认知和流行度也因为新选择的出现而持续变化,客户的倾向也在不断演变...
Third Edition of Kalman Filtering with Real-Time Appli- cations. Interval Kalman Filtering (Chapter 10) is added to ex- pand the capability' of Kalman filtering to uncertain systems, and Wavelet ...
ACC-SO(3)-Constrained Kalman Filtering with Application to Attitude
Adaptive Filtering Primer with Matlab Adaptive Filtering Primer with Matlab Adaptive Filtering Primer with Matlab Adaptive Filtering Primer with Matlab
《SuRF: 实践性范围查询过滤与快速简洁的Trie》是关于一种新的数据结构SuRF(Succinct Range Filter)的研究论文。SuRF旨在解决传统Bloom Filter无法同时高效处理单键查找和范围查询的问题。文章由来自卡内基梅隆...
该文件是一份关于在Matlab软件包中实现卡尔曼滤波器和平滑器的最优滤波工具箱的使用手册,版本1.3,由Jouni Hartikainen、Arno Solin和Simo Särkkä编著。文档介绍了多种适用于离散时间状态空间模型的滤波方法,...
A priori knowledge of target detection probability is of critical importance in the Gaussian mixture probability hypothesis density (PHD) and cardinalized PHD (CPHD) filters.... addition, these two ...
Optimal_filtering_with_Kalman_filters_and_smoother 本文档总结了Kalman滤波器和平滑器在 Matlab 工具箱 EKF/UKF 中的应用,旨在提供一个关于Kalman滤波器的详细指南。 kalman滤波器的优点 Kalman滤波器是一种...
从提供的文件信息来看,文章的标题为"Decentralized Quantized Kalman Filtering With Scalable Communication Cost",这表明文章主要讨论的是关于分布式量化卡尔曼滤波算法以及其可扩展通信成本的研究。根据描述,...
With the recent development of high-speed computers, the Kalman filter has become more useful even for very complicated real-time applications. In spite of its importance, the mathematical theory of ...
3. 卡尔曼滤波的实时应用第四版(Kalman Filtering with Real-Time Applications 4th Edition):这本书是查尔斯·K·崔(Charles K. Chui)教授和陈关荣(Guanrong Chen)教授共同编写的。第四版中增加了两个现代...
將推薦系統的數學模型整理成簡單易懂的說明 A Summary of Collaborative Filtering Model with Factorization , Neighborhood, and Baseline approaches (Math model Cheat Sheet)
【标题】中所提到的“Optimal Kalman Filtering for System with Unknown Inputs”意味着这篇文章将探讨在存在未知输入的情况下的卡尔曼滤波器的最优滤波算法。描述中提到的方法与传统的固定未知量估计方法不同,...
自适应滤波 matlab;著者 alexander ramadan
Kalman滤波英文版的专业书籍,该书详细介绍了Kalman滤波的相关理论知识并给出的配套的matlab代码,非常适合入门和专业人士。
3-a) Gaussian filtering with different values ​ ​ ecrats types of noise Gaussian 3-b) Gaussian filtering with different values ​ ​ ecrats types of noise pulse Question 4 4 - a...