可惜的是,其Asp.Net的数据源控件 XpoDataSource,却不知怎么设计时居然漏了一个排序的功能,如果用XPCollection来做的话,那还要手工实现增、删、改、翻页等功能,查找官方网站,说是将在以后的版本中添加,没办法,只有先自己动手解决项目的燃眉之急了。
Reflect出Devexpress.XPO的源码,然后就简单了:)
将 XpoDataSource.cs, XpoDataSourceView.cs两个文件复制到自己的项目中,修改名称空间,
在 XpoDataSource.cs中,添加以下几行:
[DefaultValue(
false
)]
public string SortExpression
{
get
{
return this .GetView().SortExpression;
}
set
{
this .GetView().SortExpression = value;
}
}
public string SortExpression
{
get
{
return this .GetView().SortExpression;
}
set
{
this .GetView().SortExpression = value;
}
}
在 XpoDataSourceView.cs中,添加以下几行:
private
string
_sortExpression;
public string SortExpression
{
get
{
return this ._sortExpression;
}
set
{
this ._sortExpression = value;
}
}
然后,在
ExecuteSelect的方法中,加入一行(红字表示):
public string SortExpression
{
get
{
return this ._sortExpression;
}
set
{
this ._sortExpression = value;
}
}
Type classType
=
BuildManager.GetType(
this
.TypeName,
false
,
true
);
arguments.SortExpression = _sortExpression;
SortingCollection sorting = null ;
if ( ! string .IsNullOrEmpty(arguments.SortExpression))
.........
最后,编译,在页面中,修改dxxpo的引用(当然,也可以将控件加到工具箱中)。
arguments.SortExpression = _sortExpression;
SortingCollection sorting = null ;
if ( ! string .IsNullOrEmpty(arguments.SortExpression))
.........
页面文件使用示例:
<%
@ Register Assembly
=
"
Showfan.XPO
"
Namespace
=
"
Showfan.XPO
"
TagPrefix = " cc1 " %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="MiddlePlaceHolder" runat ="server" >
< cc1:XpoDataSource ID ="XpoExamResult" runat ="server" SortExpression ="Oid,Paper.StartTime DESC" TypeName ="XXXXXX.Component.ExamResult" >
</ cc1:XpoDataSource >
< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" DataKeyNames ="Oid" DataSourceID ="XpoExamResult" Caption ="考试列表" Width ="90%" >
< Columns >
< asp:BoundField DataField ="Paper.Name" HeaderText ="试卷名称" />
< asp:BoundField DataField ="Paper.StartTime" HeaderText ="开考时间" />
< asp:BoundField DataField ="Paper.EndTime" HeaderText ="结束时间" />
< asp:BoundField DataField ="Paper.Duration" HeaderText ="考试时长" />
< asp:BoundField DataField ="StartTime" HeaderText ="开始时间" />
< asp:BoundField DataField ="Duration" HeaderText ="用时" />
< asp:BoundField DataField ="Point" HeaderText ="得分" />
< asp:TemplateField HeaderText ="状态" >
< ItemTemplate >
<% # GetName( Eval ( " Status " )) %>
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
</ asp:Content >
TagPrefix = " cc1 " %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="MiddlePlaceHolder" runat ="server" >
< cc1:XpoDataSource ID ="XpoExamResult" runat ="server" SortExpression ="Oid,Paper.StartTime DESC" TypeName ="XXXXXX.Component.ExamResult" >
</ cc1:XpoDataSource >
< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" DataKeyNames ="Oid" DataSourceID ="XpoExamResult" Caption ="考试列表" Width ="90%" >
< Columns >
< asp:BoundField DataField ="Paper.Name" HeaderText ="试卷名称" />
< asp:BoundField DataField ="Paper.StartTime" HeaderText ="开考时间" />
< asp:BoundField DataField ="Paper.EndTime" HeaderText ="结束时间" />
< asp:BoundField DataField ="Paper.Duration" HeaderText ="考试时长" />
< asp:BoundField DataField ="StartTime" HeaderText ="开始时间" />
< asp:BoundField DataField ="Duration" HeaderText ="用时" />
< asp:BoundField DataField ="Point" HeaderText ="得分" />
< asp:TemplateField HeaderText ="状态" >
< ItemTemplate >
<% # GetName( Eval ( " Status " )) %>
</ ItemTemplate >
</ asp:TemplateField >
</ Columns >
</ asp:GridView >
</ asp:Content >
如果用了App_Theme的话,在skin文件中,加入:
<
asp:GridView
runat
="server"
CssClass
="GridTb"
AllowPaging
="True"
PageSize
="15"
>
那就一劳永逸了,连GridView的翻页、排序等等功能,在页面中都不需要逐个设置了。
但是使用过程中,却丢失了智能标记,属性窗口等可视化编辑的功能,也没时间多整它了,算是下一个版本出来前的临时解决方案吧:D
支持排序的XpoDatasource源码下载