页面加载时对Gridview进行排序 [英] Sorting Gridview when pageloads
问题描述
我想在网页上对GridView进行排序
加载.
目前,用户需要点击
标头文字.
我希望这种情况能够自动发生吗?
任何帮助都将得到应用!
Hi,
I want to sort my GridView when the webpage
loads up.
At the moment the user needs to click on the
Headers text.
I want that to happen automattically?
Any help would be appriciated!!
推荐答案
尝试以下一项:
Try this one:
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection) ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression,string direction)
{
// You can cache the DataTable for improving performance
DataTable dt = GetData().Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridView1.DataSource = dv;
GridView1.DataBind();
}
另外,添加SortExpression =< value here =">"在客户端脚本中,并且AllowSorting = True到您的gridview属性.
供您参考:
http://stackoverflow.com/questions/702600/sorting-and-paging- with-gridview-asp-net [ ^ ]
如果这解决了您的问题,请标记为答案.
-Eduard
also, add SortExpression="<value here="">" in your client side script and AllowSorting=True to your gridview property.
For your reference:
http://stackoverflow.com/questions/702600/sorting-and-paging-with-gridview-asp-net[^]
Please mark as answer if this solved your problem.
-Eduard
对自身的查询中的gridview数据进行排序,并在页面加载事件中加载gridview.
希望对我有帮助
sort the gridview data in its query it self and load the gridview in page load event.
Hpoe this helps
我认为Eduard Lu做到了,我们有相同的解决方案,而且效果很好,我想这就是您所需要的,
希望对您有帮助!
谢谢,
i think Eduard Lu got it, we have the same solution and it works good, i think this is what you need,
hope it helps!
thanks,
这篇关于页面加载时对Gridview进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!