步骤:
1.页面上首先设置datagrid的AllowSorting="true",以及指定排序方法OnSortCommand="DataGrid1_SortCommand"
2.在列的header处指定排序字段如SortExpression="keyword"
3.后台cs部分 本例使用了ViewState["orderBy"]来保存排序字段和规则( SetSql();是我实例中的datagrid绑定方法)
PS:绑定datagrid后,要修改列SortExpression中相反的排序顺序
特别提示:
i.OnSortCommand、OnSelectCommand、OnDeleteCommand和OnUpdateCommand方法是在OnItemCommand执行之后起作用。如DataGrid1_ItemCommand中的处理是要排除Header的,有必要做如下处理:
CodeDataGrid1_ItemCommand方法
ii.排序后,重新绑定datagrid也是不可缺少的功课。
附录:
MS对DataGrid、DataList和Repeater的ListItemType,自动有AlternatingItem的解释 Even if you have not created an in your Repeater control, every other row still has an ItemType of ListItemType. . Therefore, you must check for both ListItemType.Item and ListItemType.AlternatingItem to process every row.
1.页面上首先设置datagrid的AllowSorting="true",以及指定排序方法OnSortCommand="DataGrid1_SortCommand"
2.在列的header处指定排序字段如SortExpression="keyword"
3.后台cs部分 本例使用了ViewState["orderBy"]来保存排序字段和规则( SetSql();是我实例中的datagrid绑定方法)
PS:绑定datagrid后,要修改列SortExpression中相反的排序顺序
1 protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
2 {
3
4 ViewState["orderBy"] = e.SortExpression;
5 SetSql();
6 //找到排序的列,并修改把它的排序属性
7 DataGridColumn clm = null;
8 for (int i = 0; i < DataGrid1.Columns.Count; i++)
9 {
10 if (DataGrid1.Columns[i].SortExpression == e.SortExpression)
11 {
12 clm = DataGrid1.Columns[i];
13 break;
14 }
15 }
16 if (clm == null) return;
17
18 if (e.SortExpression.ToLower().IndexOf("desc") > 0)
19 {
20 clm.SortExpression = e.SortExpression.ToLower().Replace("desc", "asc");
21 }
22 else
23 {
24 if (e.SortExpression.ToLower().IndexOf("asc") > 0)
25 {
26 clm.SortExpression = e.SortExpression.ToLower().Replace("asc", "desc");
27 }
28 else
29 {
30 clm.SortExpression = e.SortExpression.ToLower() + " desc";
31 }
32 }
33
34 }
2 {
3
4 ViewState["orderBy"] = e.SortExpression;
5 SetSql();
6 //找到排序的列,并修改把它的排序属性
7 DataGridColumn clm = null;
8 for (int i = 0; i < DataGrid1.Columns.Count; i++)
9 {
10 if (DataGrid1.Columns[i].SortExpression == e.SortExpression)
11 {
12 clm = DataGrid1.Columns[i];
13 break;
14 }
15 }
16 if (clm == null) return;
17
18 if (e.SortExpression.ToLower().IndexOf("desc") > 0)
19 {
20 clm.SortExpression = e.SortExpression.ToLower().Replace("desc", "asc");
21 }
22 else
23 {
24 if (e.SortExpression.ToLower().IndexOf("asc") > 0)
25 {
26 clm.SortExpression = e.SortExpression.ToLower().Replace("asc", "desc");
27 }
28 else
29 {
30 clm.SortExpression = e.SortExpression.ToLower() + " desc";
31 }
32 }
33
34 }
特别提示:
i.OnSortCommand、OnSelectCommand、OnDeleteCommand和OnUpdateCommand方法是在OnItemCommand执行之后起作用。如DataGrid1_ItemCommand中的处理是要排除Header的,有必要做如下处理:
CodeDataGrid1_ItemCommand方法
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item||e.Item.ItemType!=ListItemType.AlternatingItem)
return;
}
{
if (e.Item.ItemType != ListItemType.Item||e.Item.ItemType!=ListItemType.AlternatingItem)
return;
}
ii.排序后,重新绑定datagrid也是不可缺少的功课。
附录:
MS对DataGrid、DataList和Repeater的ListItemType,自动有AlternatingItem的解释 Even if you have not created an
※不望朝进大步§※§但求日积点滴※