GridView排序——微软提供Sort

在GridView中,根据其中的某列进行排序。

1. 页面:AllowSorting=“True” onsorting=“ ”,SortExpression="列名"


  
< asp:GridView ID = " grdResult " runat = " server " CssClass = " gridviewstyle " AutoGenerateColumns = " False "

AllowSorting
= " True " onsorting = " grdResult_Sorting " >

< Columns >
< asp:CommandField HeaderText = " Edit " ShowEditButton = " True " >
< ControlStyle Width = " 150px " />
</ asp:CommandField >
< asp:BoundField DataField = " Id " HeaderText = " ID " SortExpression = " Id " >
< ControlStyle Width = " 20px " />
< HeaderStyle Width = " 80px " ForeColor = " White " />
</ asp:BoundField >
< asp:BoundField DataField = " Name " HeaderText = " Name " >
< ControlStyle Width = " 50px " />
< HeaderStyle Width = " 120px " Font - Underline = " True " />
</ asp:BoundField >
</ Columns >

</ asp:GridView >

2.后台代码:

ContractedBlock.gif ExpandedBlockStart.gif View Code

   
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
this .grdResult.Attributes.Add( " SortExpression " , " Id " );
this .grdResult.Attributes.Add( " SortDirection " , " ASC " );
BindGridInfo();
}
}

  
private List < Test > GetTestData()
{
Test test1
= new Test { Id = 1 , Name = " Test1 " };
Test test2
= new Test { Id = 2 , Name = " Test2 " };
Test test3
= new Test { Id = 3 , Name = " Test3 " };
Test test4
= new Test { Id = 4 , Name = " Test4 " };
List
< Test > lstTest = new List < Test > ();
lstTest.Add(test1);
lstTest.Add(test2);
lstTest.Add(test3);
lstTest.Add(test4);
return lstTest;
}
private DataTable GetData()
{
DataTable dtTest
= new DataTable();
dtTest.Columns.Add(
" Id " );
dtTest.Columns.Add(
" Name " );
dtTest.Rows.Add(
1 , " 111 " );
dtTest.Rows.Add(
3 , " 333 " );
dtTest.Rows.Add(
2 , " 222 " );
dtTest.Rows.Add(
4 , " 444 " );

return dtTest;
}
// 数据绑定,如果返回数据源是DataTable则可以直接排序,如果不是则要先转换为DataTable格式数据源
private void BindGridInfo()
{
List
< Test > lstTest = GetTestData();
DataTable dt
= new DataTable();
dt.Columns.Add(
" Id " );
dt.Columns.Add(
" Name " );
for ( int i = 0 ; i < lstTest.Count; i ++ )
{
DataRow dr
= dt.NewRow();
dr[
" Id " ] = lstTest[i].Id;
dr[
" Name " ] = lstTest[i].Name;
dt.Rows.Add(dr);
}
string sortExpression = this .grdResult.Attributes[ " SortExpression " ];
string sortDirection = this .grdResult.Attributes[ " SortDirection " ];

DataTable dtSource
= GetData();
if (( ! string .IsNullOrEmpty(sortExpression)) && ( ! string .IsNullOrEmpty(sortDirection)))
{
dt.DefaultView.Sort
= string .Format( " {0} {1} " , sortExpression, sortDirection);
}
grdResult.DataSource
= dt; // dtSource;
grdResult.DataBind();
}
ContractedBlock.gif ExpandedBlockStart.gif Class Test: View Code

   
/// <summary>
/// define a container class
/// </summary>
private class Test
{
private int _id;
/// <summary>
/// test id
/// </summary>
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
/// <summary>
/// test name
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
}

  
/// <summary>
/// sorting
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void grdResult_Sorting( object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression.ToString();
string sortDirection = " ASC " ;
if (sortExpression == this .grdResult.Attributes[ " SortExpression " ])
{
sortDirection
= ( this .grdResult.Attributes[ " SortDirection " ].ToString() == sortDirection) ? " DESC " : " ASC " ;
}
this .grdResult.Attributes[ " SortExpression " ] = sortExpression;
this .grdResult.Attributes[ " SortDirection " ] = sortDirection;
this .BindGridInfo();
}

 参考: http://www.cnblogs.com/heekui/archive/2008/06/02/1212051.html

转载于:https://www.cnblogs.com/eva_2010/articles/1995646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值