<asp:GridView ID="GridPayCondition" Width="750px" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowSorting="True" OnRowDataBound="GridPayCondition_RowDataBound" AllowPaging="True" OnPageIndexChanging="GridPayCondition_PageIndexChanging" OnSorting="GridPayCondition_Sorting" >
aspx文件的代码:
cs文件的代码:
protected void GridPayCondition_Sorting(object sender, GridViewSortEventArgs e)
{
List<PayCondition> paycondition = new List<PayCondition>();
EditPayConditionLogic epcLogic = new EditPayConditionLogic();
paycondition = epcLogic.GetPayCondition();
if(paycondition!=null)
{
var param = Expression.Parameter(typeof(PayCondition), e.SortExpression);
var sortExpression = Expression.Lambda<Func<PayCondition, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);
if(ViewState["SortDirection"]=="DESC")
{
ViewState["SortDirection"] = "ASC";
GridPayCondition.DataSource = paycondition.AsQueryable<PayCondition>().OrderBy(sortExpression);
}
else
{
ViewState["SortDirection"] = "DESC";
GridPayCondition.DataSource = paycondition.AsQueryable<PayCondition>().OrderByDescending(sortExpression);
}
GridPayCondition.DataBind();
}
}
答案 0 :(得分:0)
错误:
数据源不支持服务器端数据分页
原因:
GridPayCondition.DataSource = paycondition.AsQueryable<PayCondition>().OrderBy(sortExpression);
<强>解决方案:强>
GridPayCondition.DataSource = paycondition.AsQueryable<PayCondition>().OrderBy(sortExpression).ToList();
<强>原因:强>
您无法使用IQueryable对象将数据绑定到GridView和 仍然使用分页和排序。您必须将List返回给GridView 使用ToList()方法。
答案 1 :(得分:0)
protected void GridPayCondition_Sorting(object sender, GridViewSortEventArgs e)
{
List<PayCondition> paycondition = new List<PayCondition>();
EditPayConditionLogic epcLogic = new EditPayConditionLogic();
paycondition1 = epcLogic.GetPayCondition();
if (paycondition1 != null)
{
var param = Expression.Parameter(typeof(PayCondition),e.SortExpression);
var sortExpression = Expression.Lambda<Func<PayCondition, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);
if ((string)ViewState["SortDirection"] == "DESC")
{
ViewState["SortDirection"] = "ASC";
GridPayCondition.DataSource = paycondition1.AsQueryable<PayCondition>().OrderBy(sortExpression).ToList();
}
else
{
ViewState["SortDirection"] = "DESC";
GridPayCondition.DataSource = paycondition1.AsQueryable<PayCondition>().OrderByDescending(sortExpression).ToList();
}
GridPayCondition.DataBind();
}