C# 允许基于gridview的列排序

C# 允许基于gridview的列排序,c#,asp.net,sorting,C#,Asp.net,Sorting,这个网格视图有两列需要排序 问题是,我正在获取一个类列表,以将数据绑定到gridview数据源,在单击网格列时,将触发排序表达式,但数据源为空,因此如何实现这一点 标记: <asp:GridView ID="grdDelegateList" runat="server" CssClass="gridviewBorder" AutoGenerateColumns="False" AllowSorting="true" EmptyDataText="Th

这个网格视图有两列需要排序

问题是,我正在获取一个类列表,以将数据绑定到gridview数据源,在单击网格列时,将触发排序表达式,但数据源为空,因此如何实现这一点

标记:

<asp:GridView ID="grdDelegateList" runat="server" CssClass="gridviewBorder" AutoGenerateColumns="False" AllowSorting="true"
                    EmptyDataText="There are no delegates assigned for this bridge." CellPadding="2"
                    Style="margin-left: 0px" BackColor="White" Font-Names="Calibri" BorderWidth="1px"
                    Width="100%" AllowPaging="True" GridLines="Horizontal" RowHoverBackColor="#666666"
                    RowHoverForeColor="White" SelectedRowStyle-BackColor="#333333" SelectedRowStyle-ForeColor="White"
                    PageSize="20" OnPageIndexChanging="grdDelegateList_PageIndexChanging" OnRowCommand="grdDelegateList_RowCommand"
                    OnRowDataBound="grdDelegateList_RowDataBound" 
                    OnRowDeleting="grdDelegateList_RowDeleting" onsorting="grdDelegateList_Sorting">
                    <Columns>
                        <asp:BoundField HeaderText="Employee ID" DataField="DelegateID" ItemStyle-HorizontalAlign="Center" SortExpression="DelegateID"
                            HeaderStyle-HorizontalAlign="Center" />
                        <asp:BoundField HeaderText="Display Name" DataField="FullName" ItemStyle-HorizontalAlign="Left" SortExpression="FullName"
                            HeaderStyle-HorizontalAlign="Left" />
                        <asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <span style="cursor: pointer">
                                    <asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
                                        Text="Remove" OnClientClick="return confirm('Are you sure you want to remove this Delegate');">
                                        <img alt="Remove" src="Images/trash.png" style="border:0;" />
                                    </asp:LinkButton></span>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

                </asp:GridView>

由于数据源为空,我是否需要再次从数据库中获取数据,如果是,我正在获取列表,那么我是否需要添加cutom逻辑以首先将列表转换为数据表,或者它是否也可以使用列表。任何建议???

由于数据源是列表,而不是当前场景中的数据表,因此您将获得空值。因此,在绑定网格时,最好使用列表中转换的数据表。其余的排序逻辑将不受影响

更新:


将数据表保留在会话中。从会话中对数据表进行排序后,在排序事件中检索并重新绑定网格。另外,不要忘记使用更新的排序表达式更新会话表。请参见

该类的列表中有另一个类的对象,需要该类的对象来获取该类的信息。因此datatable将不起作用…那么您应该使用将数据源转换为列表而不是datatable,并对列表项执行排序。我已经对代码进行了更改,现在它正在获取数据表,但在排序表达式事件中griddatasource仍然为null。。
 protected void grdDelegateList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable table = grdDelegateList.DataSource as DataTable;//this is coming                 null

            if (table != null)
            {
                DataView dataView = new DataView(table);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);

                grdDelegateList.DataSource = dataView;
                grdDelegateList.DataBind();
            }
        }

private string ConvertSortDirection(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }