在asp.net 2.0中,如果是使用gridview的话,可以要对gridview进行排序的话,可以配合sqldatasource来使用,已经内建了排序的功能(即点列标题进行排序),但如果要配合其他数据源控件的话,则必须使用
自定义的排序方法了,今天学习到了如何用dataset搭配gridview的话,进行标题排序,现将其方法摘录如下: 首先,用一个方法将数据取出来,放到DATASET里去,如下代码 private DataSet GetData() { SqlConnection myConnection = new SqlConnection(ConnectionString); SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories", myConnection); DataSet ds = new DataSet(); ad.Fill(ds); return ds; } get { if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = SortDirection.Ascending; return (SortDirection) ViewState["sortDirection"]; } set { ViewState["sortDirection"] = value; } } string sortExpression = e.SortExpression; if (GridViewSortDirection == SortDirection.Ascending) { GridViewSortDirection = SortDirection.Descending; SortGridView(sortExpression, DESCENDING); } else { GridViewSortDirection = SortDirection.Ascending; SortGridView(sortExpression, ASCENDING); } } DataTable dt = GetData().Tables[0]; DataView dv = new DataView(dt); dv.Sort = sortExpression + direction; GridView1.DataSource = dv; GridView1.DataBind(); }
GridView启用了排序,但是标题字段不能单击。。。9首先: |