0

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

问题是我正在获取一个类列表以将数据绑定到 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>

排序表达式事件:

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

由于数据源为空,我是否需要再次从 db 中获取数据,如果是,我正在获取列表,所以我需要添加自定义逻辑以首先将列表转换为数据表还是可以使用也列出来。有什么建议么??

4

1 回答 1

0

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

更新:

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

于 2013-08-12T09:58:39.500 回答