我需要在名为LastName的字段的aspx页面中对asp:gridView中的行进行排序。
我使用C#中的数据表绑定网格。我不在aspx文件中使用数据源。
在网格中提供SortExpression不起作用。
任何人都可以指导我如何实现这一目标?
到目前为止我已经完成了这件事,但它似乎没有排序
<asp:GridView ID="gdPlayersList" runat="server" AllowSorting="true"
AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#F9F9F9"
Font-Size="X-Small" Font-Names="Verdana" Font-Bold="False"
DataKeyNames="PlayerId,CasinoId"
HeaderStyle-BorderWidth="1px" HeaderStyle-BackColor="LightGray"
HeaderStyle-BorderStyle="Solid" RowStyle-BorderColor="#666666"
RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px"
RowStyle-ForeColor="#666666" OnRowDataBound="Grid_RowDataBound"
OnSorting="Grid_Onsorting"
EmptyDataText="No records found" >
<Columns>
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName"
SortExpression="LastName"/>
<asp:BoundField HeaderText="Date opened" DataField="DateOpened"
DataFormatString="{0:mm/dd/YYYY}" HtmlEncode="false"
NullDisplayText="1/1/0001" />
</Columns>
</asp:GridView>
C# code:
protected void Grid_Onsorting(object sender, GridViewSortEventArgs e)
{
DataTable sourceTable = gdPlayersList.DataSource as DataTable;
DataView view = new DataView(sourceTable);
view.Sort = e.SortExpression + " " + "DESC";
this.ViewState["sortExpression"] = e.SortExpression + " " + "DESC";
}
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
...
}
答案 0 :(得分:2)
你有没看过GridView.Sorting事件?
处理_Sorting
事件,重新分配,然后DataBind()
。
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
gv.DataSource = dt;
gv.DataBind();
答案 1 :(得分:0)
DataTable tab = DataGrid.DataSource as DataTable;
tab.DefaultView.Sort = columnName + " " + sortOrder;
tab = tab.DefaultView.ToTable();
在上面的示例中,tab
将被排序