Gridview使用Linq查询数据源排序

时间:2014-05-06 17:47:05

标签: sorting gridview

我尝试按State和DateandTime列对网格视图进行排序。我没有设置gridView的DataSourceID属性,因此我使用ViewState变量跟踪sortexpression和排序方向。 DatabindgOrgEventListing中的状态和方向是正确的,但gridview不显示已排序。

    <asp:UpdatePanel ID="uplOrgEventListing" runat="server">
    <ContentTemplate>
        <asp:GridView ID="grdOrgEventListing" runat="server" DataKeyNames="Id" AutoGenerateColumns="false" 
            CssClass="gridorgeventlisting" AlternatingRowStyle-CssClass="gridorgeventlistingaltrow" AllowSorting="true"
            OnSelectedIndexChanged="grdOrgEventListing_SelectedIndexChanged" OnSorting="grdOrgEventListing_Sorting">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="College Fair Name">
                    <ItemStyle Width="375px" />
                </asp:BoundField>
                <asp:BoundField DataField="City" HeaderText="City">
                    <ItemStyle Width="135px" />
                </asp:BoundField>
                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State">
                    <ItemStyle Width="35px" />
                </asp:BoundField>
           <asp:BoundField DataField="DateAndTime" HeaderText="Date &amp; Time" SortExpression="DateAndTime">
                    <ItemStyle Width="290px" />
                </asp:BoundField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlState" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>



    private string GetSortDirection(string column)
    {
        string sortDirection = "DESC";
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "DESC"))
                {
                    sortDirection = "ASC";
                }
            }

        }
        else
            sortDirection = "ASC";

        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }

    protected void grdOrgEventListing_Sorting(object sender, GridViewSortEventArgs e)
    {
        GetSortDirection(e.SortExpression);
        DatabindgOrgEventListing();
     }




   private void DatabindgOrgEventListing()
    {
        List<OrgEvent> orgEvents;
        List<OrgEventListing> orgEvents2;

        using (TlMainEntities context = new TlMainEntities())
        {
            context.States.MergeOption = MergeOption.NoTracking;
            context.OrgEvents.MergeOption = MergeOption.NoTracking;

            if (ddlState.SelectedIndex > 0)
                orgEvents = OrgEventsByStateQuery(
                    context, Convert.ToInt32(ddlState.SelectedValue), DateTime.Now).ToList();
            else
                orgEvents = OrgEventsQuery(context, DateTime.Now).ToList();
        }

        string sortDirection = ViewState["SortDirection"] as string;
        string sortExpression = ViewState["SortExpression"] as string;

        orgEvents2 =
                (from orge in orgEvents
                 select new OrgEventListing
                 {
                     Id = orge.Id,
                     City = orge.City,
                     State = TlMainUtilities.StateSelectCode(orge.StateId),
                     Name = orge.Name,
                     DateAndTime = (
                        orge.StartDate != null ?
                        ((DateTime)orge.StartDate).ToString("ddd, MMM dd, yyyy") + " " +
                        ((DateTime)orge.StartDate).ToString("h:mmtt").ToLower() + " - " + (
                            orge.EndDate != null ?
                            ((DateTime)orge.EndDate).ToString("h:mmtt").ToLower() :
                            String.Empty
                        ) :
                        String.Empty
                     )
                 }).ToList();
        if (sortExpression == "State")
        {
            if (sortDirection == "ASC")
                orgEvents2.OrderBy(x => x.State).ToList();
            else
                orgEvents2.OrderByDescending(x => x.State).ToList();
        }
        else
        {
            if (sortDirection == "ASC")
                orgEvents2.OrderBy(x => x.DateAndTime);
            else
                orgEvents2.OrderByDescending(x => x.DateAndTime);
        }
        //orgEvents2.Sort();
        grdOrgEventListing.DataSource = orgEvents2;

        grdOrgEventListing.DataBind();
    }

1 个答案:

答案 0 :(得分:0)

我更改了以下代码

        if (sortExpression == "State")
    {
        if (sortDirection == "ASC")
            orgEvents2.OrderBy(x => x.State).ToList();
        else
            orgEvents2.OrderByDescending(x => x.State).ToList();
    }
    else
    {
        if (sortDirection == "ASC")
            orgEvents2.OrderBy(x => x.DateAndTime);
        else
            orgEvents2.OrderByDescending(x => x.DateAndTime);

到此

            if (sortExpression == "State")
        {
            if (sortDirection == "ASC")
                orgEvents2 = orgEvents2.OrderBy(OrgEventListing => OrgEventListing.State).ToList();
            else
            {
                orgEvents2 = orgEvents2.OrderByDescending(OrgEventListing => OrgEventListing.State).ToList();
            }
        }
        else
        {
            if (sortDirection == "ASC")
                orgEvents2 = orgEvents2.OrderBy(OrgEventListing => OrgEventListing.DateAndTime).ToList();
            else
                orgEvents2 = orgEvents2.OrderByDescending(OrgEventListing => OrgEventListing.DateAndTime).ToList();
        }