【问题标题】:Sorting a Gridview using Datatable使用 Datatable 对 Gridview 进行排序
【发布时间】:2011-11-11 02:37:33
【问题描述】:

我有一个gridview,我想在单击它的任何列标题时对其进行排序。有一个在运行时构建并分配给 gridview 以填充数据的 DataTable。这是 DataTable 和 Gridview:

DataTable dtMedication = new DataTable();
dtClientMedications.Columns.Add("Id");
dtClientMedications.Columns.Add("BrandName");
dtClientMedications.Columns.Add("GenericName");
dtClientMedications.Columns.Add("Dosage");
dtClientMedications.Columns.Add("Physician");
dtClientMedications.Columns.Add("DatePrescribed");
dtClientMedications.Columns.Add("Status");
dtClientMedications.Columns.Add("ClientMedicationDataId");

<asp:GridView ID="gdvMainList" runat="server" AutoGenerateColumns="False"
                            SkinID="PagedGridView" onrowcommand="gdvMainList_RowCommand" 
                            DataKeyNames="Id" onsorting="gdvMainList_Sorting">
                            <PagerStyle CssClass="gridpager" HorizontalAlign="Right" />
                            <Columns>
                                <ucc:CommandFieldControl HeaderText="Actions" ShowDeleteButton="true" ButtonType="Image"
                                    DeleteImageUrl="~/App_Themes/Default/images/delete.png" ShowEditButton="true"
                                    EditImageUrl="~/App_Themes/Default/images/edit.png" DeleteConfirmationText="Are you sure you want to delete?">
                                    <ItemStyle HorizontalAlign="Center" Width="60px" />
                                </ucc:CommandFieldControl>
                                <asp:BoundField DataField="BrandName" HeaderText="Brand Name" />
                                <asp:BoundField DataField="GenericName" HeaderText="Generic Name" />
                                <asp:BoundField DataField="Dosage" HeaderText="Dosage" />
                                <asp:BoundField DataField="Physician" HeaderText="Physician" />
                                <asp:BoundField DataField="DatePrescribed" HeaderText="Date Prescribed" />
                                <asp:BoundField DataField="Status" HeaderText="Status" />
                                <asp:TemplateField HeaderText="">
                                    <ItemStyle CssClass="HiddenCol" Width="0px" />
                                    <HeaderStyle CssClass="HiddenCol" />
                                    <ItemTemplate>
                                        <asp:HiddenField ID="hdfClientMedicationDataId" runat="server" Value='<%#Bind("ClientMedicationDataId") %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <EmptyDataTemplate>
                                <div class="divEmptyGrid">
                                    --- No Medication Exists ---
                                </div>
                            </EmptyDataTemplate>
                        </asp:GridView>

【问题讨论】:

  • 这样做有什么问题?你在 gdvMainList_Sorting 中有什么?
  • 我还没有在那个事件处理程序中写任何东西。问题是我不知道如何根据我的网格和数据表处理该事件

标签: c# asp.net sorting gridview datatable


【解决方案1】:

为了对网格视图的行进行排序,您应该使用 SortDescription 类构造函数并将两个参数传递给该类,即 sortby 值和排序方向。详情请参阅Sort gridrows based on column click

【讨论】:

    【解决方案2】:

    第一步,将GridViewAllowSorting 属性设置为true,然后为要用于排序的每一列添加SortExpression 属性:

    SortExpression 属性表示应为的表达式 用于在单击该字段的排序标题链接时对数据进行排序

    让我们从上面的代码中考虑BoundField,我添加了一个SortExpression 属性,其值设置为BrandName,这意味着当BrandName 的列标题将被单击时@987654331 中的列“品牌名称” @ 将用于对数据进行排序:

    现在在gdvMainList_Sorting 事件中,您必须将网格重新绑定到已排序的数据:

    protected void gdvMainList_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
    {
        //Using DataView for sorting DataTable's data
        DataView view = dtMedication.DefaultView;
        view.Sort = String.Format("{0} {1}", e.SortExpression, GetSortingDirection());
        gdvMainList.DataSource = view;
        gdvMainList.DataBind();
    }
    

    如果您注意到我使用了 getSortingDirection(),该方法返回 'ASC' 或 'DESC' 以分别按升序或降序对数据进行排序。

    protected string GetSortingDirection() 
    {
        if(ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "ASC";
        else if(ViewState["SortDirection"] == "ASC")
            ViewState["SortDirection"] = "DESC";
        else
            ViewState["SortDirection"] = "ASC";
    
        return ViewState["SortDirection"];
    }
    

    一些有用的链接:

    1. GridView sorting using VB.net
    2. Sorting and Paging tutorial

    【讨论】:

    • 我在排序事件处理程序中添加的一个代码片段是 if(viewState["SortColumn"] e.SortExpressions) ViewState["sortDirection"] = null; ViewState["SortColumn"] = e.SortExpression;当您单击不同的列时,这默认返回为 ASC
    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2017-12-14
    • 2015-04-01
    相关资源
    最近更新 更多