Custom data method - GridView Sorting and Paging
One of the common task when you use custom data methods is sorting and paging. There is an article here on sorting and paging using custom data. Based on the feed back received from developers to elaborate the source code and better explain this article is written.
Note that custom method expected to return a data table object in this approach. In this article Search() is custom method that returns a DataTable object.
1: #region GridView Page Index Changing
2:
3: /// <summary>
4: /// Handle Gridview paging event
5: /// and bind Search results data to GridView
6: /// </summary>
7: /// <param name="sender"></param>
8: /// <param name="e"></param>
9: protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
10: {
11: GridView1.PageIndex = e.NewPageIndex;
12: //Bind search reuslts
13: GridView1.DataSource = SortDataTable(Search() as DataTable, true);
14: GridView1.DataBind();
15:
16: }
17:
18: #endregion
19:
20: #region Properties SortDirection and SortExpresssion
21:
22: /// <summary>
23: /// property GridView Sort Direction
24: /// </summary>
25: private string GridViewSortDirection
26: {
27: get { return ViewState["SortDirection"] as string ?? "ASC"; }
28: set { ViewState["SortDirection"] = value; }
29:
30: }
31: /// <summary>
32: /// GridView sort expression property
33: /// </summary>
34: private string GridViewSortExpression
35: {
36: get { return ViewState["SortExpression"] as string ?? string.Empty; }
37: set { ViewState["SortExpression"] = value; }
38: }
39: #endregion
40:
41: #region SortDirection method
42: /// <summary>
43: /// Get current sortDirection and switch between
44: /// ascending and decending order
45: /// </summary>
46: /// <returns></returns>
47: private string GetSortDirection()
48: {
49: switch (GridViewSortDirection)
50: {
51: case "ASC":
52: GridViewSortDirection = "DESC";
53: break;
54: case "DESC":
55: GridViewSortDirection = "ASC";
56: break;
57: }
58:
59: return GridViewSortDirection;
60: }
61:
62: #endregion
63:
64: #region SortDataTable
65:
66: /// <summary>
67: /// Sort Data Table when Page Index changing
68: /// </summary>
69: /// <param name="dataTable"></param>
70: /// <param name="isPageIndexChanging"></param>
71: /// <returns></returns>
72: protected DataView SortDataTable(DataTable dataTable,
bool isPageIndexChanging)
73: {
74: if (dataTable != null)
75: {
76:
77: DataView dataView = new DataView(dataTable);
78: if (GridViewSortExpression != string.Empty)
79: {
80: if (isPageIndexChanging)
81: {
82: dataView.Sort = string.Format("{0} {1}",
GridViewSortExpression, GridViewSortDirection);
83:
84: }
85: else
86: {
87: dataView.Sort = string.Format("{0} {1}",
GridViewSortExpression, GetSortDirection());
88:
89: }
90:
91: }
92: return dataView;
93: }
94: else
95: {
96: return new DataView();
97: }
98:
99:
100: }
101: #endregion
102:
103:
104: #region GridView columns sorting
105:
106:
107: /// <summary>
108: /// Handle page sorting
109: /// </summary>
110: /// <param name="sender"></param>
111: /// <param name="e"></param>
112: protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
113: {
114:
115: //Get sortExpression
116: GridViewSortExpression = e.SortExpression;
117: int pageIndex = GridView1.PageIndex;
118: //Bind GridView with search results
119: GridView1.DataSource = SortDataTable(Search() as DataTable, false);
120: GridView1.DataBind();
121: //Set the page Index
122: pageIndex = GridView1.PageIndex;
123:
124:
125: }
126:
127: #endregion
Let me know any comments. I make sure to better explain or resolve any bugs related to this article.