gridview排序问题
使用gridview时候碰到一个排序问题,似乎用其自带排序不能解决
在使用中不停需要添加或删除数据到gridview中,这里使用的是DataTable,而gridview本身还包含了一个模版列(内含一个lable显示文字,一个imageButton提供点击链接)在这种情况下尝试使用其自带的排序功能结果发现没有效果!
不知道有没有人遇到过类似的问题,还望指教
------解决方案--------------------ASP.NET2.0中GRIDVIEW控件完全代码实现模版列排序!(代码调试已通过)
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 //初始化页面绑定数据到GridView
6 GetDataSet();
7 GridViewBind();
8 }
9 }
10
11 /// <summary>
12 /// 生成ds数据集
13 /// </summary>
14 private DataSet GetDataSet()
15 {
16 //string strSQL = "SELECT * FROM MANAGER.DB1 ";
17 string strSQL = "SELECT * FROM MANAGER.DB1 ";
18 DataSet ds = conn.GetDs(strSQL);
19 return ds;
20 }
21
22 /// <summary>
23 /// 绑定数据到GridView
24 /// </summary>
25 private void GridViewBind()
26 {
27 this.GridView1.DataSource = this.GetDataSet().Tables[0].DefaultView;
28 this.GridView1.DataBind();
29 }
30 /// <summary>
31 /// 存储选定列当前排序状态
32 /// </summary>
33 public SortDirection GridViewSortDirection
34 {
35 get
36 {
37 if (ViewState[ "sortDirection "] == null)
38 ViewState[ "sortDirection "] = SortDirection.Ascending;
39 return (SortDirection)ViewState[ "sortDirection "];
40 }
41 set { ViewState[ "sortDirection "] = value; }
42 }
43
44 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
45 {
46 string sortExpression = e.SortExpression;
47 if (GridViewSortDirection == SortDirection.Ascending)
48 {
49 GridViewSortDirection = SortDirection.Descending;
50 SortGridView(sortExpression, "DESC ");
51 }
52 else
53 {
54 GridViewSortDirection = SortDirection.Ascending;
55 SortGridView(sortExpression, "ASC ");
56 }
57 }
58
59 /// <summary>
60 /// 排序并绑定
61 /// </summary>
62 private void SortGridView(string sortExpression, string direction)
63 {
64 DataTable dt = this.GetDataSet().Tables[0];
65 DataView dv = new DataView(dt);
66
67 dv.Sort = sortExpression + " " + direction;
68
69 GridView1.DataSource = dv;
70 GridView1.DataBind();
71 }