我开发了一个具有Gridview控件的asp.net应用程序。当我点击asp.net中的Gridview头文本时,我想要它进行排序。实体框架用于绑定gridview。
protected void grdmortgagesaver_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
var databind = from i in mortgageentites.Clients orderby i.LastName select i;
if (databind.Count() > 0)
{
grdmortgagesaver.DataSource = databind.ToList();
grdmortgagesaver.DataBind();
DataTable dt = clients;
if (dt != null)
{
// DataTable dt = ds.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grdmortgagesaver.DataSource = dv;
grdmortgagesaver.DataBind();
}
}
}
答案 0 :(得分:4)
如果您使用的是标准ASP.NET System.Web.UI.WebControls.GridView,则可以在属性窗格中通过将“AllowSorting”设置为true或通过“弹出”来简单地“启用排序”通过选中“启用排序”
来控制右上角答案 1 :(得分:1)
在gridview控件中添加AllowSorting =“true”并
protected void grd_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.SortExpression == hdfSortExp.Value)
{
if (hdfUpDown.Value == "1")
hdfUpDown.Value = "0";
else
hdfUpDown.Value = "1";
}
else //New Column clicked so the default sort direction will be incorporated
hdfUpDown.Value = "0";
hdfSortExp.Value = e.SortExpression; //Update the sort column
BindGrid(hdfSortExp.Value, this.CBool(hdfUpDown.Value));
}
在您的c#代码中
public void BindGrid(string sortBy, bool inAsc)
{
grd.DataSource = WManager.GetAdminTags(txtSearch.Text.Trim(), sortBy, inAsc);
grd.DataBind();
}
答案 2 :(得分:0)
首先在GridView声明标签的页面中添加属性AllowSorting="true"
,然后在Gridview_SortCommand中尝试以下代码作为指导
try {
if (SortType == false) {
SortView.Sort = e.SortExpression + " ASC";
//dviewGeneral.Sort = e.SortExpression & " ASC"
Session.Item("SortOrder") = SortView.Sort.ToString;
SortType = true;
} else {
SortView.Sort = e.SortExpression + " DESC";
Session.Item("SortOrder") = SortView.Sort.ToString;
//dviewGeneral.Sort = e.SortExpression & " DESC"
SortType = false;
}
CMSgrid.SelectedIndex = -1;
CMSgrid.CurrentPageIndex = 0;
SortBind();
} catch (Exception ex) {
UserMsgBox(ex.ToString());
}