C# GridView排序将页面重置为原始数据
伙计们,我正在使用VS2005C和SQLServer2005 我有一个对我的GridView的搜索,我想使用GridView的排序功能对我的搜索结果进行排序 但是,每次过滤结果后,当我按标题对DataGrid进行排序时,它总是将GridView重置为表中的原始完整数据 例如 “我的网页”加载一个gridview,其中包含58名学生的数据 b我在名称下搜索James,18个结果显示在GridView中 我按header类按类对结果进行排序 d GridView刷新并返回到58名学生的原始完整列表 我试过: 在单独的网页上实现搜索表,这样它就不会与原始的gridview冲突 将gridview名称更改为其他名称 我意识到: 当我将指针悬停在标题上方时,它将始终显示 javascript:_doPostBack'ctl00$MainContent$GridView1,排序$Issue 即使我可能已将GridView1更改为另一个名称C# GridView排序将页面重置为原始数据,c#,asp.net,sql,visual-studio-2005,C#,Asp.net,Sql,Visual Studio 2005,伙计们,我正在使用VS2005C和SQLServer2005 我有一个对我的GridView的搜索,我想使用GridView的排序功能对我的搜索结果进行排序 但是,每次过滤结果后,当我按标题对DataGrid进行排序时,它总是将GridView重置为表中的原始完整数据 例如 “我的网页”加载一个gridview,其中包含58名学生的数据 b我在名称下搜索James,18个结果显示在GridView中 我按header类按类对结果进行排序 d GridView刷新并返回到58名学生的原始完整列表
我需要一些在gridview中对搜索结果进行排序的解决方案,而不是在排序时带回原始数据。我已经实现了使用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();
}
如果有任何疑问,请告诉我。假设在页面加载中调用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);
}
遵循下面的代码…希望它有帮助你能得到点还是没有我不能得到我想要的结果这就是为什么我问你是否尝试过答案中给出的解决方案我试图在方法之外声明两个变量,但无法声明。我应该在哪里声明它们?您需要在页面中将它们声明为私有变量或私有属性。。。。。。