排序使用模板和boundfields的gridview的问题

时间:2011-11-02 12:50:13

标签: c# asp.net sorting gridview

JLAdjustmentList.aspx.cs

    protected void Page_Load(object sender, EventArgs e) {
            LoadApprovalList();
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            gridAdjustments.DataBind();
        }

        protected void gridAdjustments_OnSorting(object sender, EventArgs e) {
            LoadApprovalList();
        }

        private void LoadApprovalList() {
            if (PermissionHelper.IsPermissionGranted(Permission.JLAdjustmentView)) {
                gridAdjustments.DataSource = Facade.AdAdjustment.GetListForAdministrator();
                Page.Title = "JL Adjustments List";
            } else if (PermissionHelper.IsPermissionGranted(Permission.JLAdjustmentApprove)) {
                gridAdjustments.DataSource =
                    Facade.AdAdjustment.GetListForAdministrator();
                Page.Title = "JL Adjustments Approval";
            } else if (PermissionHelper.IsPermissionGranted(Permission.JLAdjustmentEdit)) {
                gridAdjustments.DataSource =
                    Facade.AdAdjustment.GetListForManager(Current.User.Id);
                Page.Title = "JL Adjustments List";
            }
        }

        protected string GetBillableHours(AdAdjustmentListData adjustmentListData) {
            return TimeFormat.MinutesToString(adjustmentListData.BillableHoursSum);
        }

        protected string GetNonBillableHours(AdAdjustmentListData adjustmentListData) {
            return TimeFormat.MinutesToString(adjustmentListData.NonBillableHoursSum);
        }

        protected string GetDetailLink(AdAdjustmentListData adjustmentListData) {
            if (PermissionHelper.IsPermissionGranted(Permission.JLAdjustmentEdit)) {
                return string.Format("~/JLAdjustmentEdit.aspx?Id={0}", adjustmentListData.AdjustmentId);
            }
            return string.Format("~/JLAdjustmentView.aspx?Id={0}", adjustmentListData.AdjustmentId);
        }
    }
}

JLAdjustmentList.aspx

<it:GridView runat="server" ID="gridAdjustments" CssClass="object_list" OnSorting="gridAdjustments_OnSorting">
        <Columns>
            <it:BoundField DataField="AdjustmentId" HeaderText="Id" SortExpression="AdjustmentId" />
            <it:BoundField DataField="Week.DisplayName" HeaderText="Week Ending Date" SortExpression="Week.DisplayName" />
            <it:BoundField DataField="EmployeeFullName" HeaderText="USERID" SortExpression="EmployeeFullName" />
            <it:BoundField DataField="DepartmentFullName" HeaderText="Department" SortExpression="DepartmentFullName" />
            <it:BoundField DataField="Portfolio" HeaderText="Portfolio" SortExpression="Portfolio" />
            <asp:TemplateField HeaderText="Billable Hours Changed">
                <ItemStyle Width="70px"></ItemStyle>
                <ItemTemplate>
                    <%# GetBillableHours((AdAdjustmentListData)Container.DataItem) %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Non-Billable Hours Changed">
                <ItemStyle Width="70px"></ItemStyle>
                <ItemTemplate>
                    <%# GetNonBillableHours((AdAdjustmentListData)Container.DataItem)%>
                </ItemTemplate>
            </asp:TemplateField>
            <it:BoundField DataField="Reason" HeaderText="Reason For Adjustment" />
            <it:BoundField DataField="StatusName" HeaderText="Status" SortExpression="StatusName" />
            <it:BoundField DataField="LastChangedDateFormatted" HeaderText="Date of Last Action" />
            <asp:TemplateField HeaderText="Details">
                <ItemTemplate><center>
                    <asp:ImageButton runat="server" ImageUrl="~/img/edit.gif" 
                        PostBackUrl='<%# GetDetailLink((AdAdjustmentListData)Container.DataItem) %>' /></center>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </it:GridView>

SortHelper.cs

 public static class SortHelper {
        private static Dictionary<Type, Dictionary<String, ITypedSorter>> cache;

        static SortHelper() {
            cache = new Dictionary<Type, Dictionary<String, ITypedSorter>>();
        }

        public static IEnumerable Sort(this IEnumerable enumerable, string propertyName, bool desc) {
            if (enumerable != null) {
                if (enumerable is IQueryable) {
                    var itemType = (enumerable as IQueryable).ElementType;
                    var sorter = EnsureSorter(itemType, propertyName);
                    return sorter.Sort(enumerable, desc);
                }
                else {
                    var enumerator = enumerable.GetEnumerator();
                    enumerator.Reset();
                    if (enumerator.MoveNext()) {
                        var itemType = enumerator.Current.GetType();
                        var sorter = EnsureSorter(itemType, propertyName);
                        return sorter.Sort(enumerable, desc);
                    }
                }
            }
            return enumerable;
        }

我不断收到“不支持指定的方法”。 AT if(enumerator.MoveNext()){

Stacktrace

  at Microsoft.Data.Extensions.Materializer`1.<Materialize>d__0.System.Collections.IEnumerator.Reset()
   at satispu.Utils.Helpers.SortHelper.Sort(IEnumerable enumerable, String propertyName, Boolean desc) in D:\Documents and Settings\aolaol\Desktop\Source_Build2\satispu.Utils\Helpers\SortHelper.cs:line 24
   at satispu.Web.Controls.GridView.OnDataBinding(EventArgs e) in D:\Documents and Settings\aolaol\Desktop\Source_Build2\satispu.Web\Controls\GridView.cs:line 119
   at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
   at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
   at System.Web.UI.WebControls.GridView.DataBind()
   at satispu.Web.JournalAdjustmentList.OnPreRender(EventArgs e) in D:\Documents and Settings\aolaol\Desktop\Source_Build2\satispu.Web\JournalAdjustmentList.aspx.cs:line 19
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

可能是什么问题或者您想要解决此问题或启用排序的是这个gridview。

Heres The Call

public IEnumerable<ADAdjustmentListData> Adjustment_GetListForAdministrator(int year) {
    return this.CreateStoreCommand(
        "GetListForAdministrator",
        CommandType.StoredProcedure,
        new SqlParameter("@Year", year)
    ).Materialize<ADAdjustmentListData>();
}




  void GridView_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e) {
            if (!String.IsNullOrEmpty(e.SortExpression)) {
                ToggleSorting(e);
            }
            if (Sorting != null) {
                e.SortDirection = SortDirection;
                Sorting(sender, e);
            }
        }

1 个答案:

答案 0 :(得分:1)

我假设您正在使用Entity Framework或LINQ to SQL ...如果是这样,您在LINQ查询中使用SortHelper,这是针对数据库的。当它这样做时,它会尝试将SortHelper转换为SQL,但它无法执行此操作,并抛出该错误。例如,如果你这样做:

var q = from o in ctx.Users select new { SortedResults = SortHelper.Sort(q) };

这会失败,因为它会尝试将SortHelper.Sort转换为SQL方法。但是只有在它不以预期的方式修改结果集时,这才能正常工作。

var q = from o in ctx.Users select o;
return SortHelper.Sort(q);

确保它可行的最好方法是在查询后调用ToList() - ToList()执行查询,然后所有内容都是LINQ to Objects操作:

var q = (from o in ctx.Users select o).ToList();
return SortHelper.Sort(q);
相关问题