GridObjectDataSource DataTable 带参数排序
GridObjectDataSource DataTable Sorting with parameters
我的问题是我不知道如何传递事件来对我的 DataTable 进行排序。
我已经列出了 .aspx 页面,以及 BindThrottles 函数后面的 .cs 文件的流程。我正在寻找一个使用 DataTable 的简单解决方案。我看到其他看起来很简单的代码使用了我最后 post 编辑的事件,但不确定如何进入该表单使用。
我的 .aspx 页面配置为..
<asp:ObjectDataSource ID="GridObjectDataSource"
runat="server" SelectMethod="BindThrottles"
TypeName="WebsiteNamespace.ThrottleInterval"
SortParameterName="SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="gvThrottles"
Name="sortDirection" PropertyName="SortDirection" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="gvThrottles" AllowSorting="True" runat="server"
DataKeyNames="UserID" DataSourceID="GridObjectDataSource"
>
<Columns>
<asp:TemplateField HeaderText="USERID" SortExpression="USERID">
<ItemTemplate>
<asp:Label ID="lblItemUserId" runat="server"
Text='<%# Eval("USERID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Requests" SortExpression="REQUESTS">
<ItemTemplate>
<asp:Label ID="lblItemNumRequests" runat="server"
Text='<%# Eval("REQUESTS") %>'></asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>
我的 BindThrottles 方法已配置...
public DataTable BindThrottles(string SortBy, int sortDirection){
//..code to run queries from MongoDB
//..code to create columns and rows of DataTable
//..code to fill the rows with the data from the MongoDB
*************************************************************
//.. I need some code here to let my DataTable sort on
//.. the column header link for Asc or Desc.
*************************************************************
}
我看到另一个 Whosebug post 显示的代码如下...
dt.DefaultView.Sort = e.SortExpression + " " +
GetSortDirection(e.SortExpression);
并且 GetSortDirection 配置为
public string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
但我不知道如何将它变成可以使用事件在已经设置了非事件参数的函数内部排序的形式,或者是否有更好的方法来对 ASC 和 DESC 进行排序按专栏 Header link.
附加信息:
我对 ASP 很陌生。我继承了这个项目,这就是为什么该功能目前仍称为 BindThrottles,甚至认为我正在尝试消除绑定(co-workers 建议)。
在此函数中,SortExpression 是列名,SortDirection 是排序顺序,即(升序或降序)。由于这两个值都存储在 ViewState 中,您可以在任何地方使用它 event.You 可以像这样使用此函数:
DataView dv = new DataView(dtTable);
dv.Sort = columnName + " " + GetSortDirection(columnName);
GridView1.DataSource = dv;
dtTable = dv.ToTable();
GridView1.DataBind();
其中 dtTable 是数据表
我的问题是我不知道如何传递事件来对我的 DataTable 进行排序。 我已经列出了 .aspx 页面,以及 BindThrottles 函数后面的 .cs 文件的流程。我正在寻找一个使用 DataTable 的简单解决方案。我看到其他看起来很简单的代码使用了我最后 post 编辑的事件,但不确定如何进入该表单使用。
我的 .aspx 页面配置为..
<asp:ObjectDataSource ID="GridObjectDataSource"
runat="server" SelectMethod="BindThrottles"
TypeName="WebsiteNamespace.ThrottleInterval"
SortParameterName="SortBy">
<SelectParameters>
<asp:ControlParameter ControlID="gvThrottles"
Name="sortDirection" PropertyName="SortDirection" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="gvThrottles" AllowSorting="True" runat="server"
DataKeyNames="UserID" DataSourceID="GridObjectDataSource"
>
<Columns>
<asp:TemplateField HeaderText="USERID" SortExpression="USERID">
<ItemTemplate>
<asp:Label ID="lblItemUserId" runat="server"
Text='<%# Eval("USERID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Requests" SortExpression="REQUESTS">
<ItemTemplate>
<asp:Label ID="lblItemNumRequests" runat="server"
Text='<%# Eval("REQUESTS") %>'></asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>
我的 BindThrottles 方法已配置...
public DataTable BindThrottles(string SortBy, int sortDirection){
//..code to run queries from MongoDB
//..code to create columns and rows of DataTable
//..code to fill the rows with the data from the MongoDB
*************************************************************
//.. I need some code here to let my DataTable sort on
//.. the column header link for Asc or Desc.
*************************************************************
}
我看到另一个 Whosebug post 显示的代码如下...
dt.DefaultView.Sort = e.SortExpression + " " +
GetSortDirection(e.SortExpression);
并且 GetSortDirection 配置为
public string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
但我不知道如何将它变成可以使用事件在已经设置了非事件参数的函数内部排序的形式,或者是否有更好的方法来对 ASC 和 DESC 进行排序按专栏 Header link.
附加信息: 我对 ASP 很陌生。我继承了这个项目,这就是为什么该功能目前仍称为 BindThrottles,甚至认为我正在尝试消除绑定(co-workers 建议)。
在此函数中,SortExpression 是列名,SortDirection 是排序顺序,即(升序或降序)。由于这两个值都存储在 ViewState 中,您可以在任何地方使用它 event.You 可以像这样使用此函数:
DataView dv = new DataView(dtTable);
dv.Sort = columnName + " " + GetSortDirection(columnName);
GridView1.DataSource = dv;
dtTable = dv.ToTable();
GridView1.DataBind();
其中 dtTable 是数据表