允许在gridview上进行基于列的排序

时间:2013-08-12 09:47:46

标签: c# asp.net sorting

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

问题是我得到一个将数据绑定到gridview数据源的类列表,点击grid列时,sort表达式会触发,但数据源为null,所以我怎样才能实现这一点。

标记:

<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>

排序表达式事件:

 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;
        }

由于数据源是null,我是否需要再次从db获取数据,如果是,我正在获取列表,所以我需要添加cutom逻辑以首先将列表转换为数据表或将它也适用于列表。有什么建议??

1 个答案:

答案 0 :(得分:0)

您将获得null,因为数据源是列表而不是当前方案中的数据表。因此,当您绑定网格时,使用列表中已转换的数据表会更好。其余的排序逻辑将不受影响。

<强>更新

将数据表保留在会话中。在从会话中对数据表进行排序后,在排序事件中检索并重新绑定网格。另外,不要忘记更新会话表以及更新的排序表达式。见http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx