在运行时从EF填充的gridview的动态排序

时间:2018-03-22 21:07:52

标签: c# asp.net .net webforms

我有一个我在运行时填充的gridview:

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" OnSorting="GridView1_Sorting" OnSorted="GridView1_Sorted"
        OnPageIndexChanging="GridView1_PageIndexChanging">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        <Columns>
        </Columns>
    </asp:GridView>

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

private void LoadData()
    {
        TSCDB = new TSCDB();

        GridView1.DataSource = TSCDB.TsCiTAllOrders
            .Where(x => x.DeletedDate == null && x.StatusName != "Cancelled")
            .AsEnumerable()
            .ToList();

        GridView1.AllowPaging = true;
        GridView1.PageSize = 10;
        GridView1.AllowSorting = true;
        GridView1.DataBind();
    }

我正在尝试在网格上启用排序,但是当我点击列标题时,此代码无效:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        var sortExpression = e.SortExpression;
        var sortDirection = e.SortDirection;

        GridView1.DataBind();
        GridView1.Sort(sortExpression, sortDirection);
    }

这显然只是在Sort&amp; amp;和DataBind声明,但令人惊讶的是,我似乎无法在Google计算机上找到明确的说明。

1 个答案:

答案 0 :(得分:0)