GridView正反向排序
if (!IsPostBack){
databind();
ViewState["Filed"] = " province";
ViewState["Sort"] = " ASC";
}
public void databind()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqltest"].ConnectionString);
cn.Open();
string sql = "SELECT * FROM Province AS P JOIN City AS C ON P.provinceID=C.FartherID ";
SqlDataAdapter sda = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
sda.Fill(ds);
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = (string)ViewState["Filed"] + "" + (string)ViewState["Sort"].ToString();//未将对象引用设置到对象的实例。
GridViewTests.DataSource = dv;
GridViewTests.DataBind();
cn.Close();
}
protected void GridViewTests_Sorting(object sender, GridViewSortEventArgs e)
{
string strPage = e.SortExpression;
if (ViewState["Filed"].ToString() == strPage)
{
if (ViewState["Sort"].ToString() == "ASC")
{
ViewState["Sort"] = "DESC";
}
else
{
ViewState["Sort"] = "Desc";
}
}
else
{
ViewState["SortOrder"] = strPage;
databind();
}
}
这是我写的GridView正反向排序,,不知道是哪里错误了里面标注的地方提示();//未将对象引用设置到对象的实例。
我写完后还没有成功请大家帮我纠正一下谢谢了 --------------------编程问答-------------------- 排序 --------------------编程问答-------------------- 给你顶 --------------------编程问答-------------------- ViewState的赋值应该在绑定之前
if (!IsPostBack)
{
ViewState["Filed"] = " province";
ViewState["Sort"] = " ASC";
databind();
} --------------------编程问答-------------------- sql语句里面order by 排序呗
--------------------编程问答-------------------- 用viewstate记录排序方式,点击一次转换 --------------------编程问答--------------------
同意,代码顺序问题
补充:.NET技术 , ASP.NET