Where are all of the steps for sorting an ASP.NET DataGrid detailed? Everytime I start to sort a grid, I have to look in several places to get all of the steps?
Although all of the steps to set up an ASP.NET DataGrid seem to be documented in one place or another, there seems to be one step missing from each description that I have found. Therefore, in attempting to simplify the process, and list every tiny detail, I probablly will miss one too, but here goes. If I should miss a step myself, remember I wrote this article because it's not a well documented, straight-forward process.
Assuming that you have placed a DataGrid on your ASP.NET page, I follow these steps.
1) Right-click the DataGrid and select the Property Builder menu option. On the General Tab, check the Allow Sorting Checkbox.
2) On the Columns Tab, for each column that you want to sort, enter the query field name of that column in the Sort Expression box. This is what turns on the underline on the respective grid column. If a column header is not underlined in the designer, it will not be sortable. The underline will only appear when you click the Apply button or the Ok button to exit the Property Builder. By the way, this is the step that some books by well-know authors leave out and it can be baffling why the header text is not underlined.
3) Create a DataView object at the class level, such as the following. Remember, in a Stateless environment, no variables are automatically saved, and therefore must be saved in Session Variables. So, I save the contents of the DataView in a Session Variable thoughout it's usage.
| private DataView dv; |
| private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if(!Page.IsPostBack) { ShowData(); } } |
| private void ShowData() { int authorID = Convert.ToInt32(Session["AuthorID"]); DateTime stDate = (DateTime)Session["StartDate"]; DateTime endDate = (DateTime)Session["EndDate"]; DataTable dt = blj.GetDictationByAuthorID(authorID, stDate, endDate); // set the dataview object to the datatable DefaultView dv = dt.DefaultView; Session["DictatedDV"] = dv; BindData(""); } |
| private void BindData(string sortExpression) { // reset the dataview, else it will be undefined value! dv=(DataView)Session["DictatedDV"]; if(sortExpression.Length>0) { dv.Sort=sortExpression; // save the dataview in stateless environment Session["DictatedDV"] = dv; } this.DataGrid1.DataSource=dv; this.DataGrid1.DataBind(); } |
| private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { BindData(e.SortExpression); } |
| private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { string sortExpression = (string)Session["SortExp"]; string sortDirection = (string)Session["SortDir"]; if(sortExpression != e.SortExpression) { sortExpression = e.SortExpression; sortDirection = "asc"; } else { if(sortDirection == "asc") sortDirection = "desc"; else sortDirection = "asc"; } Session["SortExp"] = sortExpression; Session["SortDir"] = sortDirection; BindDataGrid(sortExpression + " " + sortDirection); } |