C# 手动栅格视图排序
我的网格视图是手动数据绑定的(用于其他工作)。阅读其他线程时,我必须管理自己的排序事件 在我的网页上单击某个列对其进行排序时,会出现以下错误: “对象引用未设置为对象的实例。” 结果是表为null,但我看不出它为什么为null 有什么想法吗C# 手动栅格视图排序,c#,asp.net,sorting,gridview,C#,Asp.net,Sorting,Gridview,我的网格视图是手动数据绑定的(用于其他工作)。阅读其他线程时,我必须管理自己的排序事件 在我的网页上单击某个列对其进行排序时,会出现以下错误: “对象引用未设置为对象的实例。” 结果是表为null,但我看不出它为什么为null 有什么想法吗 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { DataTable table = GetData(); table.DefaultView
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable table = GetData();
table.DefaultView.Sort = e.SortExpression + " " + SetSortDirection(e.SortDirection.ToString());
GridView1.DataSource = table;
GridView1.DataBind();
}
编辑:
谢谢
DataTable table = BuildInfo_GridView.DataSource as DataTable;
像这样,你不会得到你已经绑定的数据源,或者当你绑定数据时,只会将它存储在视图状态并获取它,请调试你的应用程序,看看
BuildInfo\u GridView.datasource
Hi的值,谢谢你的回复。我已经更新了我的问题,使用当前代码,我只能按一种方式排序,因此,当我分配回数据时,无论我做什么,它都会显示e.SortDirection=“升序”。
Save your sort order in view state
if (ViewState["sortOrder"] != null)
{
ViewState["sortExpression"] = e.SortExpression;
if (ViewState["sortOrder"].ToString().ToUpper() == "ASCENDING")
{
e.SortDirection = SortDirection.Descending;
ViewState["sortOrder"] = SortDirection.Descending.ToString();
}
else
{
e.SortDirection = SortDirection.Ascending;
ViewState["sortOrder"] = SortDirection.Ascending.ToString();
}
}
else
{
ViewState["sortExpression"] = e.SortExpression;
ViewState["sortOrder"] = e.SortDirection.ToString();
}
DataTable table = BuildInfo_GridView.DataSource as DataTable;
Save your sort order in view state
if (ViewState["sortOrder"] != null)
{
ViewState["sortExpression"] = e.SortExpression;
if (ViewState["sortOrder"].ToString().ToUpper() == "ASCENDING")
{
e.SortDirection = SortDirection.Descending;
ViewState["sortOrder"] = SortDirection.Descending.ToString();
}
else
{
e.SortDirection = SortDirection.Ascending;
ViewState["sortOrder"] = SortDirection.Ascending.ToString();
}
}
else
{
ViewState["sortExpression"] = e.SortExpression;
ViewState["sortOrder"] = e.SortDirection.ToString();
}