我们,我正在使用VS2005 C#和SQL Server 2005。
我的 GridView 有搜索,我想使用GridView的 Sort 函数来对搜索结果进行排序。< / p>
但是,每次我对结果进行过滤后,当我按下标题对我的 DataGrid 进行排序时,它总是会将 GridView 重置为原始的完整数据。桌子。
E.g。
a)我的网页加载了一个包含58名学生数据的网格视图。
b)我在 Names 下搜索 James ,并在GridView中显示18个结果。
c)我按下标题 Class 按类对结果进行排序。
d)GridView刷新并返回原来的58名学生的完整列表。
我试过了:
在单独的网页上实施搜索表,使其不会与原始网格视图发生冲突。
将gridview名称更改为其他名称。
我意识到:
当我将指针悬停在标题上方时,它将始终显示
javascript:_doPostBack('ctl00$MainContent$GridView1,Sort$Issue)
,
即使我可能已将GridView1更改为其他名称
我需要一些解决方案来排序gridview中的搜索结果,而不是在我对其进行排序时将原始数据恢复。
答案 0 :(得分:1)
我已经使用ViewState实现了以下列方式存储排序方向(ASC或DESC):
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
listBindByName(); //this would be your procedure to look for the data you want
DataSet dsSortTable = GridView1.DataSource as DataSet;
DataTable dtSortTable = dsSortTable.Tables[0];
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
ViewState["sortExpression"] = e.SortExpression;
GridView1.DataSource = dvSortedView;
GridView1.DataBind();
}
UpdatePanel1.Update();
}
private string getSortDirectionString()
{
if (ViewState["sortDirection"] == null)
{
ViewState["sortDirection"] = "ASC";
}
else
{
if (ViewState["sortDirection"].ToString() == "ASC")
{
ViewState["sortDirection"] = "DESC";
return ViewState["sortDirection"].ToString();
}
if (ViewState["sortDirection"].ToString() == "DESC")
{
ViewState["sortDirection"] = "ASC";
return ViewState["sortDirection"].ToString();
}
}
return ViewState["sortDirection"].ToString();
}
如果有任何疑问,请告诉我。
答案 1 :(得分:0)
假设您在Page load
中为Bind调用gridview时 protected void Page_Load(object sender, EventArgs e)
{
fillgrid(false, string.Empty, false); //
}
public void fillgrid(bool sorting, string sortexpression, bool sortdir)
{
//binding codes
var data = from item in DBcontent.Tablename
select new
{
Title = item.Title
}
if (sorting)
{
if (sortexpression == "Title")
{
if (sortdir)
{
GrieviewID.DataSource = data.OrderBy(id => id.Title).ToList();
}
else
{
GrieviewID.DataSource = data.OrderByDescending(id => id.Title).ToList();
}
}
else
{
GrdID.DataSource = data.OrderByDescending(id => id.StartDate).ToList();
}
GrdID.DataBind();
}
protected void grdevents_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdevents.PageIndex = e.NewPageIndex;
BindAndSortGrid();
}
/// <summary>
/// Gets the sort direction.
/// </summary>
/// <param name="column">The column.</param>
/// <returns></returns>
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
/// <summary>
/// Bind and sort grid.
/// </summary>
private void BindAndSortGrid()
{
bool sortdir;
if (ViewState["SortDirection"] != null && ViewState["SortExpression"] != null)
{
sortdir = ViewState["SortDirection"].ToString() == "ASC" ? true : false;
fillgrid(true, ViewState["SortExpression"].ToString(), sortdir);
}
else
fillgrid(false, string.Empty, false);
}
protected void grdevents_Sorting(object sender, GridViewSortEventArgs e)
{
bool sortdir = GetSortDirection(e.SortExpression) == "ASC" ? true : false;
fillgrid(true, e.SortExpression.ToString(), sortdir);
}
按照以下代码...希望得到帮助