【发布时间】:2014-03-09 12:36:18
【问题描述】:
我的网格视图是手动数据绑定的(为了其他工作)。阅读其他线程我必须管理自己的排序事件。
在我的网页上单击列对其进行排序时,出现错误:
“对象引用未设置为对象的实例。”
结果是该表为空,但我不明白为什么它为空。
有什么想法吗?
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();
}
.
protected string SetSortDirection(string currentsortDirection)
{
string sortDirection;
if (currentsortDirection == "Ascending")
{
sortDirection = "Descending";
}
else
{
sortDirection = "Ascending";
}
return sortDirection;
}
编辑:
public DataTable GetData()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True");
conn.Open();
string query = "SELECT * FROM tablex WHERE Property='" + Request.QueryString["xxx"] + "'";
SqlCommand cmd = new SqlCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
谢谢。
【问题讨论】:
-
请调试您的应用程序并查看
BuildInfo_GridView.DataSource的值
标签: c# asp.net sorting gridview