我有一个 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>
最佳答案
第一步是将 GridView
的 AllowSorting
属性设置为 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"];
}
一些有用的链接:
关于c# - 使用数据表对 Gridview 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7316597/