【问题标题】:allow column based sorting on gridview允许在 gridview 上进行基于列的排序
【发布时间】:2013-08-12 09:47:46
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# asp.net sorting


    【解决方案1】:

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

    更新:

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

    【讨论】:

    • 类的列表有一个其他类的对象,需要获取该类的信息。所以数据表将不起作用...
    • 那么您应该使用将数据源转换为列表而不是 Datatable 并对列表项执行排序。
    • 我已经对代码进行了更改,现在它正在获取数据表,但在排序表达式事件中,griddatasource 仍然为空..
    猜你喜欢
    • 1970-01-01
    • 2011-07-18
    • 2012-06-02
    • 2015-10-22
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2018-09-20
    相关资源
    最近更新 更多