当我尝试手动对表格进行排序时,出现以下错误:
必须在使用DataView之前设置DataTable。
代码是:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable sourceTable = GridView1.DataSource as DataTable;
DataView view = new DataView(sourceTable);
string[] sortData = Session["sortExpression"]!= null ? Session["sortExpression"].ToString().Trim().Split(' ') : null;
if (sortData != null && e.SortExpression == sortData[0])
{
if (sortData[1] == "ASC")
{
view.Sort = e.SortExpression + " " + "DESC";
Session["sortExpression"] = e.SortExpression + " " + "DESC";
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
Session["sortExpression"] = e.SortExpression + " " + "ASC";
}
}
else
{
view.Sort = e.SortExpression + " " + "ASC";
Session["sortExpression"] = e.SortExpression + " " + "ASC";
}
}
我哪里错了?
最佳答案
很有可能GridView1.DataSource为null。
您可以在此处找到有关Gridview排序的教程:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx