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 932原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 1037原文链接 http://johnholliday.net/ ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1105原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1298原文链接 http://blogs.msdn.com/b/ ... -
How to work around bugs in the SPGridView control
2011-10-11 16:10 839原文链接 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 959原文链接 http://www.nearinfinity. ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 955原文链接 http://www.csharpest.net ... -
Adding CheckBoxes in SharePoint GridView (SPGridView)
2011-10-10 17:01 978原文链接 http://www.c-sharpcorner ... -
SharePoint的列级安全性
2011-10-10 11:25 1166原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1747原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1434原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1305原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 908原文链接 http://msdn.microsoft ...
相关推荐
2023年全国大学生英语竞赛样题(C类)样题答案及听力原文
出纳考核表
基于多种天气因素的光伏电站太阳能辐射量预测系统——采用人工神经网络与离线优化算法,MATLAB代码:考虑多种天气条件下光伏电站太阳能辐射量预测 关键词:辐射量预测 光伏预测 多种天气因素 参考文档:《Solar Radiation Prediction and Energy Allocation for Energy Harvesting Base Stations》 仿真平台:MATLAB+CPLEX 平台 优势:代码具有一定的深度和创新性,注释清晰,非烂大街的代码,非常精品 主要内容:代码主要做的是如何利用预测光伏电站太阳能辐射量的问题,利用人工神经网络对对其内太阳辐射量进行预测,并对无云天气以及多云天气进行了分别讨论,与线性模型相比该模型具有更好的性能,除此之外,代码还研究了太阳能的分配问题,采用离线优化算法和四种在线启发式算法分别进行分配策略的优化,并利用太阳辐射数据评估了算法的性能。 该代码适合新手学习以及在此基础上进行拓展,代码质量非常高,出图效果极佳 ,核心关键词: 1. 光伏电站太阳能辐射量预测 2. 多种天气因素 3. 人工神经网络 4. 预测模型 5. 线性
数据结构实验实习指导书(c语言)
"lyh不会打代码"生存小有戏改版
站群系统/泛目录站群源码/泛站群cms系统【小说泛目录站群源码】 效果截图和演示https://www.lxsjfx.cn/3181.html 绿茶小说站群2.x-秒收隔天速出权重-小说流量稳定收割机-精品轻量级PHP站群系统站群系统,小说行业专用引流精品站群,绿茶小说站群为独立站群系统(无需依托CMS),独立的整篇小说优化内容库(拒绝句子拼凑),模板自适应PC端和移动端,流量一起做! 1、绿茶小说站群为独立站群系统(无需依托CMS) 2、对域名要求不高,百元域名均可操作 3、独立的首页、列表页、小说阅读页 4、独立的整篇小说优化内容库(拒绝句子拼凑) 5、可自定页面后缀(html、shtml、xml…..) 6、拒绝全站404跳转到内容页 7、还有强大的网站XML地图功能,便于链接提交 8、模板自适应PC端和移动端,流量一起做! 站群系统/泛目录站群源码/泛站群cms系统【小说泛目录站群源码】
IQC检验员(来料检验员)绩效考核表
2024年全球AI应用趋势年度报告
安全生产绩效考核表
04-【标准制度】公司 KPI 绩效考核流程
第14讲:深入理解指针(4)
考虑用户舒适度的冷热电多能互补综合能源系统优化调度模型:结合PMV衡量与碳排放交易机制的MATLAB仿真实现,考虑用户舒适度的冷热电多能互补综合能源系统优化调度 MATLAB代码:考虑用户舒适度的冷热电多能互补综合能源系统优化调度 关键词:用户舒适度 综合能源 PMV 优化调度 参考文档:《冷热电气多能互补的微能源网鲁棒优化调度》基础模型加舒适度部分模型; 仿真平台:MATLAB+yalmip+cplex 主要内容:代码主要做的是考虑用户舒适度的冷热电多能互补综合能源系统优化调度模型,在传统的冷热电联供型综合能源系统的基础上,进一步考虑了热惯性以及用户的舒适度,并用预测平均投票数PMV对用户的舒适度进行衡量,且通过改变PMV的数值,可以对比不同舒适度要求对于综合能源系统调度结果的影响。 同时,代码还补充性的考虑了碳排放交易机制,并设置经济性最优以及碳排放最优两种对比场景,从而丰富算例,效果非常明显。 使用matlab+yalmip+cplex进行代码的 ,考虑用户舒适度; 综合能源系统; PMV; 优化调度; 冷热电多能互补; 碳排放交易机制。,考虑用户舒适度与碳排放交易的冷热电多能
内容概要:本文详细阐述了利用ANSI转义码在Xshell脚本中进行光标的灵活操控方法。介绍了从光标的隐藏、定位(特定行/列)、保存位置、复位、清除以及显示控制的基本命令,重点描述了如何使用以上提到的功能构建实用的UI组件——文本模式下工作的进度条。文中提供的简单实例演示了一个完整的循环逻辑,它能动态刷新视图,在每一次迭代中根据程序实际进展更新屏幕上的表现形式,同时保持界面美观性和易读性。并且提到由于不同的终端可能有不同的兼容情况,脚本的跨环境行为可能存在细微差别。 适合人群:初学者至中级水平的技术爱好者或者软件开发者,尤其是希望深入掌握Linux环境下命令行工具使用者。 使用场景及目标:① 学习并理解Xshell脚本里涉及的ANSI转义码概念和技术点,从而增强对终端界面元素(如菜单、提示符等)的操作技能;② 掌握通过程序手段构造动态变化的CLI应用程序技巧,比如实时跟踪长时间任务的状态; 阅读建议:本文不仅包含了具体命令的学习,更展示了它们是如何组合起来创造复杂视觉反馈机制的案例研究。对于想进一步探索终端开发领域的程序员而言,这无疑提供了很好的入门指引材料。考虑到各种操作系统上支持度的问题,在测试代码之前应当确认自己的工作平台已经正确配置好。
内容概要:该文档详细探讨了针对达梦数据库的各种性能优化技术和处理方法。具体包括回表问题及其解决措施如覆盖索引和FAST POOL机制;变量窥探、统计数据收集优化方法,例如设置统计桶数量和采样子表数目;视图上拉、JOIN优化、EXISTS与NOT EXISTS子查询重写策略;分区裁剪和多KEY哈希等方面的深入探讨,提供了多个具体的优化技巧,旨在帮助用户有效提升SQL执行性能,并解决了多种可能导致性能下降的关键因素。 适合人群:数据库管理员、运维工程师及具有一定经验的数据开发人员等,尤其是负责使用和维护基于达梦数据库系统的技术团队成员。 使用场景及目标:适用于希望通过改善查询速度来提高系统响应时间的专业人士;需要处理大型数据库或复杂查询的任务;或是正在寻找改进现有数据库架构的方法的机构。它还特别针对那些希望确保最优硬件资源利用率的人群。 其他说明:本文档不仅介绍了理论性的背景知识和技术细节,还包括了大量的实际案例演示和参数调整建议,方便读者理解和实践这些优化方法。此外,针对每种优化策略提供了详细的指导,使得即使是对某些高级特性较为陌生的读者也能顺利掌握关键技能。
54 -营销部经理绩效考核表1
外贸部绩效考核表格
选择使用如下方法,增加系统盘自由空间。最简模式:完成2、4②,即可全面清除电脑垃圾、痕迹。 1、将“桌面”、“我的文档”以及系统盘的其它地方保存的个人文件资料,转移到别的盘保存。 2、双击桌面“计算机”,“系统磁盘”右键--属性--常规/工具:
岗位绩效考核评定表excel表格模板
1、文件内容:apache-commons-vfs-javadoc-2.0-11.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/apache-commons-vfs-javadoc-2.0-11.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装