c# - 使用数据表对 Gridview 进行排序

标签 c# asp.net sorting gridview datatable

我有一个 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>

enter image description here

最佳答案

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

SortExpression property indicates the expression that should be used to sort the data when that field's sorting header link is clicked

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

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

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

关于c# - 使用数据表对 Gridview 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7316597/

相关文章:

c# - 增加自动完成扩展列表的宽度

javascript - 如何使用 JavaScript 在 asp.net 中基于另一个文本框的文本框上进行验证?

scala - 如何按键的长度对 scala 映射进行排序(假设键是字符串)

python - 如何在 Python 中使用自定义谓词排序

c# - 使用主页时在网页上设置背景图片

c# - WPF 指向路径几何的点

c# - 如何以编程方式选择 webBrowser 控件中的文本? C#

c# - ASP.NET MVC 将参数从 View 传递到 Controller

PHP 数组排序设置数字然后从 1

c# - 如何在不立即读取整个文件的情况下编辑文本文件