gpt4 book ai didi

c# - 如何使用下拉列表对 gridview 进行排序

转载 作者:行者123 更新时间:2023-11-30 15:45:26 24 4
gpt4 key购买 nike

我指的具体例子是这个网站

http://www.iamextreme.net/category.php?CategoryId=1&SubCategoryId=0&SortBy=name

注意下拉列表的排序方式。基本上我想根据价格、名称、受欢迎程度对产品进行排序??

我是否必须重新查询数据库并再次在 gridview 中检索排序的数据,或者我是否应该已经对 gridview 中已经存在的项目进行排序?

我试过这个,但这是错误的方法,基本上我试图在页面加载事件中重新填充数据,这会出错。

现在什么是正确的做法。

最佳答案

您可以在 DropDownList 的 SelectIndexChanged 事件中使用类似这样的东西

protected void DropDownList1_SelectIndexChanged(object sender, EventArgs e)
{
gvSorting.Sort(DropDownList1.SelectedValue, SortDirection.Ascending);
}

并处理GridView的排序事件

protected void gvSorting_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtSortTable = gvSorting.DataSource as DataTable;
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
gvSorting.DataSource = dvSortedView;
gvSorting.DataBind();
}
}

private static string getSortDirectionString(SortDirection sortDireciton)
{
string newSortDirection = String.Empty;
if (sortDireciton == SortDirection.Ascending)
{
newSortDirection = "ASC";
}
else
{
newSortDirection = "DESC";
}
return newSortDirection;
}

关于c# - 如何使用下拉列表对 gridview 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com