protected void GridView_ReferedDataDetail_Sorting(object sender, GridViewSortEventArgs e) { DataTable DataTable_GridView_ReferedDataDetail = (DataTable)ViewState["DataTable_GridView_ReferedDataDetail"]; DataView DataViewRefereDataDetail = DataTable_GridView_ReferedDataDetail.DefaultView; if (ViewState["DataTable_GridView_ReferedDataDetail"]!= null) { if (ViewState["GridView_ReferedDataDetail_SortDirection"].ToString() == "Asc") { ViewState["GridView_ReferedDataDetail_SortDirection"] = "Desc"; string MySortDirection = "Desc"; string MySortExpression = e.SortExpression + " " + MySortDirection; DataViewRefereDataDetail.Sort = MySortExpression; } else { ViewState["GridView_ReferedDataDetail_SortDirection"] = "Asc"; string MySortDirection = "Asc"; string MySortExpression = e.SortExpression + " " + MySortDirection; DataViewRefereDataDetail.Sort = MySortExpression; } } GridView_ReferedDataDetail.DataSource = DataViewRefereDataDetail; GridView_ReferedDataDetail.DataBind(); }
其中红色部分是在GridView的数据绑定时,在ViewState中记录GridView的试图状态
体会:
1、在写程序或检查错误时一定要细心。比如我在方法一中就将 " "写成了"",将其中的空格丢弃了
2、差错时,Visual Studio给出的错误和排错提示很重要,一定要仔细看看。
3、对于GridView,如果其数据源不是数据源控件,而是在后台程序中自定义的数据源,在进行数据绑定的同时,应该在ViewState中记录GridView的试图状态(如果需要的话,比如用于Sorting事件),否则,在GridView的Sorting事件中再调用GridView的相关属性将是Null
4、GridView的有关属性可能都是针对数据源控件的,比如在
- C# code
- protected void GridView_ReferedDataDetail_Sorting(object sender, GridViewSortEventArgs e)