使用Datatable对Gridview进行排序

时间:2011-09-06 07:52:07

标签: c# asp.net sorting gridview datatable

我有一个gridview,我想在点击任何列标题时对其进行排序。 DataTable是在运行时构建的,并分配给gridview来填充数据。这是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

2 个答案:

答案 0 :(得分:9)

AllowSorting的{​​{1}}属性设置为GridView的第一步,然后为您要用于排序的每列添加true属性:

  

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

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

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

gdvMainList_Sorting

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

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

一些有用的链接:

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

答案 1 :(得分:0)

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