先看看SPGridView能实现的效果:
1)分组,分页
![](https://i-blog.csdnimg.cn/blog_migrate/ee38c16d829d015758b23b410ee75ffc.gif)
2)下拉菜单
![](https://i-blog.csdnimg.cn/blog_migrate/e60f648355f54c372d488bf2d6253802.gif)
3)过滤
![](https://i-blog.csdnimg.cn/blog_migrate/959a4cbff765f5396d360dee3cda8bd8.gif)
以上效果是不是跟系统的列表一模一样?
SPGridView继承于GrieView,拥有GridView的所有功能,并扩展了以上功能。
下面讲述SPGridView的两种用法:采用自定义数据源和采用WSS的列表作为数据源。
利用SPGridView显示自定义数据源
Step1:
新建一个页面,写入如下内容:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page MasterPageFile="~/_layouts/application.master" Language="C#" AutoEventWireup="true" CodeFile="SPGridViewTest.aspx.cs" Inherits="_Default"
%>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1"
%>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
asp:Content
ID
="Content1"
ContentPlaceHolderID
="PlaceHolderMain"
runat
="server"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
cc1:SPGridView
ID
="SPGridView1"
runat
="server"
AutoGenerateColumns
="False"
AllowPaging
="True"
PageSize
="3"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
AlternatingRowStyle
CssClass
="ms-alternating"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
SelectedRowStyle
CssClass
="ms-selectednav"
Font-Bold
="True"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
cc1:SPGridView
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
div
style
="text-align:center"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
cc1:SPGridViewPager
ID
="SPGridViewPager1"
runat
="server"
GridViewId
="SPGridView1"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
cc1:SPGridViewPager
></
div
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
asp:Content
>
注意:
SPGridView不支持自动生成列,所以 一定要设置AutoGenerateColums为false。
SPGridView直接启用了分页,并且,页面放入一个SPGrieViewPager,来实现分页。
Step2:
在CodeFile里写一个提供测试数据的函数:
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
public
DataTable GetDataTable()
{
DataTable tblData = new DataTable();
tblData.Columns.Add("Title");
tblData.Columns.Add("Author");
tblData.Columns.Add("Rating");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return tblData;
}
Step3:
在Page_Load里写代码,加入ObjectDataSource控件,用这个控件来调用上面的GetDateTable函数。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ObjectDataSource odsDataSource
=
new
ObjectDataSource();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
odsDataSource.ID
=
"
ExampleSource
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
odsDataSource.TypeName
=
this
.GetType().FullName
+
"
,
"
+
this
.GetType().Assembly.FullName;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
odsDataSource.SelectMethod
=
"
GetDataTable
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Controls.Add(odsDataSource);
有人可能要奇怪了,为什么要用ObjectDataSource来调用GetDataTable,直接给SPGridView的DataSource属性赋值不行吗?
答案是:可以的。但是,那样的话,你就不的不再写一些代码来处理SPGridView的过滤和排序事件 。而ObjectDataSource是可以自动来处理这些事件的。
Step4:继续在Page_Load写代码,设置SPGridView的过滤格式和过滤属性,如果不这样设置的话,过滤功能不会起效(这个设置可是我起早摸黑,用reflector看它的代码才发现的...)。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
{0}表示过滤值,{1}表示过滤字段值
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.FilteredDataSourcePropertyFormat
=
"
{1}='{0}'
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.FilteredDataSourcePropertyName
=
"
FilterExpression
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.EnableViewState
=
false
;
Step4:继续在Page_Load写代码,添加字段,并启用排序 :
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
添加字段
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPBoundField col
=
new
SPBoundField();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.DataField
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.SortExpression
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.HeaderText
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.Columns.Add(col);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col
=
new
SPBoundField();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.DataField
=
"
Author
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.SortExpression
=
"
Author
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.HeaderText
=
"
Author
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.Columns.Add(col);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col
=
new
SPBoundField();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.DataField
=
"
Rating
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.SortExpression
=
"
Rating
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
col.HeaderText
=
"
Rating
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.Columns.Add(col);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.AllowSorting
=
true
;
Step5:设置过滤字段:
SPGridView1.AllowFiltering
=
true
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.FilterDataFields
=
"
,Author,Rating
"
;
注意 :开始的列如果不启用过滤需要加 “,”。
Step6:继续在Page_Load写代码,设置分组:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
设置分组
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.AllowGrouping
=
true
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.AllowGroupCollapse
=
true
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.GroupField
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.GroupDescriptionField
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.GroupFieldDisplayName
=
"
分组:
"
;
Step7:Continue...,添加一个有下拉菜单的字段:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
添加下拉菜单字段
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPMenuField colMenu
=
new
SPMenuField();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.HeaderText
=
"
Presenter Name
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.TextFields
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
指定关联的菜单ID
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.MenuTemplateId
=
"
PresenterListMenu
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.NavigateUrlFields
=
"
Title,Rating
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.NavigateUrlFormat
=
"
do.aspx?p={0}&q={1}
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.TokenNameAndValueFields
=
"
EDIT=Title,NAME=Rating
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
colMenu.SortExpression
=
"
Title
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
菜单
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
MenuTemplate presenterListMenu
=
new
MenuTemplate();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
presenterListMenu.ID
=
"
PresenterListMenu
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
菜单项
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
MenuItemTemplate biogMenu
=
new
MenuItemTemplate(
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
"
Read Biography
"
,
"
/_layouts/images/EawfNewUser.gif
"
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
biogMenu.ClientOnClickNavigateUrl
=
"
do.aspx?this=%EDIT%&that=%NAME%
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
entry.ClientOnClickScript = "your javascript here";
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
presenterListMenu.Controls.Add(biogMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
菜单项
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
MenuItemTemplate broadcastMenu
=
new
MenuItemTemplate(
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
"
Recent Broadcasts
"
,
"
/_layouts/images/ICWM.gif
"
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
presenterListMenu.Controls.Add(broadcastMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
分割线
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
MenuSeparatorTemplate sepMenu
=
new
MenuSeparatorTemplate();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
presenterListMenu.Controls.Add(sepMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
菜单项
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
MenuItemTemplate favMenu
=
new
MenuItemTemplate(
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
"
Add to Favorites
"
,
"
/_layouts/images/addtofavorites.gif
"
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
presenterListMenu.Controls.Add(favMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(presenterListMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPGridView1.Columns.Add(colMenu);
Step8:绑定数据源:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
绑定数据源
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.SPGridView1.DataSourceID
=
"
ExampleSource
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.SPGridView1.DataBind();
OK,可以看效果了~
注意:SPGridView一定要在WSS的环境中使用,所以各位做测试的时候需要把页面放到layouts中。
整个Page_Load的代码如下:
protected void Page_Load(object sender, EventArgs e)
{
ObjectDataSource odsDataSource = new ObjectDataSource();
odsDataSource.ID = "ExampleSource";
odsDataSource.TypeName = this.GetType().FullName + "," + this.GetType().Assembly.FullName;
odsDataSource.SelectMethod = "GetDataTable";
Controls.Add(odsDataSource);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//{0}表示过滤值,{1}表示过滤字段值
SPGridView1.FilteredDataSourcePropertyFormat = "{1}='{0}'";
SPGridView1.FilteredDataSourcePropertyName = "FilterExpression" ;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SPGridView1.EnableViewState = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加字段
SPBoundField col = new SPBoundField();
col.DataField = "Title";
col.SortExpression = "Title";
col.HeaderText = "Title";
SPGridView1.Columns.Add(col);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
col = new SPBoundField();
col.DataField = "Author";
col.SortExpression = "Author";
col.HeaderText = "Author";
SPGridView1.Columns.Add(col);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
col = new SPBoundField();
col.DataField = "Rating";
col.SortExpression = "Rating";
col.HeaderText = "Rating";
SPGridView1.Columns.Add(col);
SPGridView1.AllowSorting = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SPGridView1.AllowFiltering = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SPGridView1.FilterDataFields = ",Author,Rating";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置分组
SPGridView1.AllowGrouping = true;
SPGridView1.AllowGroupCollapse = true;
SPGridView1.GroupField = "Title";
SPGridView1.GroupDescriptionField = "Title";
SPGridView1.GroupFieldDisplayName = "分组:";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加下拉菜单字段
SPMenuField colMenu = new SPMenuField();
colMenu.HeaderText = "Presenter Name";
colMenu.TextFields = "Title";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//指定关联的菜单ID
colMenu.MenuTemplateId = "PresenterListMenu";
colMenu.NavigateUrlFields = "Title,Rating";
colMenu.NavigateUrlFormat = "do.aspx?p={0}&q={1}";
colMenu.TokenNameAndValueFields = "EDIT=Title,NAME=Rating";
colMenu.SortExpression = "Title";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//菜单
MenuTemplate presenterListMenu = new MenuTemplate();
presenterListMenu.ID = "PresenterListMenu";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//菜单项
MenuItemTemplate biogMenu = new MenuItemTemplate(
"Read Biography", "/_layouts/images/EawfNewUser.gif");
biogMenu.ClientOnClickNavigateUrl = "do.aspx?this=%EDIT%&that=%NAME%";
//entry.ClientOnClickScript = "your javascript here";
presenterListMenu.Controls.Add(biogMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//菜单项
MenuItemTemplate broadcastMenu = new MenuItemTemplate(
"Recent Broadcasts", "/_layouts/images/ICWM.gif");
presenterListMenu.Controls.Add(broadcastMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//分割线
MenuSeparatorTemplate sepMenu = new MenuSeparatorTemplate();
presenterListMenu.Controls.Add(sepMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//菜单项
MenuItemTemplate favMenu = new MenuItemTemplate(
"Add to Favorites", "/_layouts/images/addtofavorites.gif");
presenterListMenu.Controls.Add(favMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(presenterListMenu);
SPGridView1.Columns.Add(colMenu);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//绑定数据源
this.SPGridView1.DataSourceID = "ExampleSource";
this.SPGridView1.DataBind();
}
利用SPGridView显示列表的数据
只要把数据源控件换成SPDataSource,然后利用SPDataSource连接相应的列表即可:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPDataSource odsDataSource
=
new
SPDataSource();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
odsDataSource.ID
=
"
ExampleSource
"
;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Controls.Add(odsDataSource);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPSite site
=
new
SPSite(
"
http://ssjin073:9032
"
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SPList list
=
site.RootWeb.GetListFromUrl(
"
http://ssjin073:9032/Lists/List/AllItems.aspx
"
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
odsDataSource.List
=
list;
整个Page_Load代码如下:
注意:此代码要正常运行,测试用的列表必须有标题和Origin两个字段。
除了用代码给SPGridView添加字段,适应于GridView的一切手段都是可以应用到SPGridView的,
比如,你可以添加一个模板字段(TemplateFiled),模板内容当然可以按需控制了。
下载所有示例代码:http://files.cnblogs.com/jianyi0115/PageTest.rar