SPGridView 研究笔记 Part 2 - 排序与过滤
来源:互联网 发布:win7看不到网络计算机 编辑:程序博客网 时间:2024/05/17 03:47
SPGridView的排序与GridView是相同的. 我们先看看SPGridView默认排序功能的效果.
启用排序功能先把将SPGridView的AllowSorting属性设置成true, 然后给需要排序的列设置SortExpression属性. 这样就可以效果页面内的排序, 就是只能把当前在SPGridView里显示的数据排序一下.
如果需要从数据源那端排序, 就得先准备好一个有排序表达式参数的取数据方法, 之前我们准备的NorthwindData.cs里已经有了一个GetProductList(string sortExpression)方法, 我们只需要再把ObjectDataSource的SortParameterName设置成与那个参数名字一置, "sortExpression"就行了.
过滤是SPGridView新增的功能, 效果很酷.
Filter菜单中装载了SPGridView中全部的Category, 选中一个Category后SPGridView就会只显示选中Category的数据, 再点开Category菜单就变成右边图那样了.
注: 最上面两个排序是Filter菜单自己带的, 即使你把SPGridView的AllowSorting设置成false并把Category列的SortExpression去掉, 这两个排序还是可以用. 而且这两个排序会先尝试利用ObjectDataSource的排序功能, 不行就在SPGridView页内排序. 呵呵. 怪怪的.
要启用SPGridView的过滤功能, 先要把SPGridView的AllowFiltering设为true, 再使用FilterDataFields属性设置要启用过滤菜单的列, 这里我们不需给前面ProductId和ProductName两个列启用过滤功能, 但是在FilterDataFields里我们还是得用逗号来充当这2个列的占位符.
<cc1:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" FilterDataFields=",,CategoryName" AllowFiltering="true">
<cc1:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" FilterDataFields=",,CategoryName" AllowFiltering="true" FilteredDataSourcePropertyFormat="{1} = '{0}'" FilteredDataSourcePropertyName="FilterExpression">
其中FilteredDataSourcePropertyName是指SPGridView调用的DataSource控件用于过滤数据的属性名称, 我们现在用的是ObjectDataSource控件, 它的FilterExpression属性是用来处理过滤的. 而FilteredDataSourcePropertyFormat里的"{1} = '{0}'"就是用来给FilterExpression赋值用的. {1}表示需要过滤的列名CategoryName, {0} 表示被选中的过滤条件 Confections (见上面右图). 当选择 Confections 时, SPGridView就会给ObjectDataSource的FilterExpression属性传入 CategoryName = 'Confections' 从而达到过滤效果.
注: 只有当SPGridView指定了一个DataSource控件时过滤才会有效, 使用DataView作数据源去设置它的RowFilter是不行的. 详情见SPGridView源代码中的DoSortPostBackEventProcessing方法.
完整代码:
<%@ Page Language="C#" MasterPageFile="~masterurl/Default.Master" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
< span>asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
SPGridView Demo: Part 2 - Sorting and Filtering
< span>asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<cc1:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" FilterDataFields=",,CategoryName" AllowFiltering="true" FilteredDataSourcePropertyFormat="{1} = '{0}'" FilteredDataSourcePropertyName="FilterExpression" AllowSorting="true">
<Columns>
<cc1:SPBoundField DataField="ProductId" HeaderText="Product ID" SortExpression="ProductId">
< span>cc1:SPBoundField>
<cc1:SPBoundField DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName" />
<cc1:SPBoundField DataField="CategoryName" HeaderText="Category" SortExpression="CategoryName">
< span>cc1:SPBoundField>
<asp:BoundField DataField="UnitPrice" DataFormatString="${0:F2}" HeaderText="Unit Price" SortExpression="UnitPrice" />
<asp:TemplateField HeaderText="Orderable" SortExpression="Discontinued">
<itemtemplate>
<asp:Label id="lblDiscontinued" runat="server" text='<%# Convert.ToBoolean(Eval("Discontinued")) ? "Yes" : "No" %>'>< span>asp:Label>
< span>itemtemplate>
< span>asp:TemplateField>
< span>Columns>
< span>cc1:SPGridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetProductList" SortParameterName="sortExpression" TypeName="SPGridView_Demo.NorthwindData">< span>asp:ObjectDataSource>
< span>asp:Content>
- SPGridView 研究笔记 Part 2 - 排序与过滤
- SPGridView 研究笔记 Part 3 - 分组
- SPGridView 研究笔记 Part 1 - 基础和项菜单
- SPGridView(实现分页,排序,过滤,菜单)
- AdvancedDataGrid排序与过滤
- Oracle过滤与排序
- 排序与过滤
- 自定义排序与过滤
- MySQL基础(2)——检索、排序与过滤
- Java字符串排序与过滤问题
- 基于Angularjs的过滤与排序
- SQL学习之排序与过滤
- QT的排序与过滤(QSortFilterProxyModel)
- Oracle过滤和排序2
- Collaborative Filtering 协同过滤小结 part-2:SVD
- 公差与配合part 2
- 公差与配合part 2
- 在Sharepoint webpart 中使用 SPGridview 实现内容的分页排序分组
- 能实现检查窗口是否存在的函数
- sepcial JS used in page
- JavaScript中confirm方法的重载
- 16个经典的面试回答思路
- 不使用installutil.exe安装WINDOWS服务
- SPGridView 研究笔记 Part 2 - 排序与过滤
- 实用保健方法
- DataBinder.Eval方法研究
- smarty的保留变量
- USB 基本知识
- JAVA多线程同步——学无止境
- Trufun UML实战系列—UML与面向对象的分析和设计!
- 安装ant随笔
- ASP.NET URL Rewrite. URL重写