Friday, March 27, 2009

How Sorting ASP.NET DataGrid

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;


4) In the Page Load event, I will place some code that looks something like this.


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
ShowData();
}
}


5) The ShowData method is responsible for getting the data and calling the BindData method.


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("");
}


6) Iwill call the BindData method to bind the dataview to the grid.


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();
}


7) Switch back to the designer and click on the DataGrid and then show the Property window for the grid. Click the events button(lighnning bolt) and double-click in the SortCommand event. That will create a DataGrid1_SortCommand event. Enter the code shown below into the event. In this event, I set the SortExpression, which you will recall is the field name of the data column in the designer. This is actually a string name of the column to sort on.


private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
BindData(e.SortExpression);
}


8) Now, we can sort, but only in one direction, ascending. At this point our grid does not support sorting in both directions. That can be solved by enhancing the code for the SortCommand event shown above. I will simplly remember the last order that we sorted in and reverse that direction with the code shown below.


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);
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home