So easy to sort a DataTable!
Up until now my applications usually return arrays of "business objects" from the middle tier and most of my data UI is geared around handling these arrays. I have a customized DataGrid which handles the paging and sorting of these arrays by default. But tonight, I had a need to sort a simple DataTable since it just wasn't worth creating thousands of "business objects" to store two attributes ... I was impressed at how simple it was to convert my DataGrid to support sorting of the DataTable.
Here is a snippet from my customized control:
if (this.DataSource is DataTable) { DataTable table = (DataTable) this.DataSource; if (sortExpression != null && sortExpression.Length > 0) { table.DefaultView.Sort = sortExpression + " " + sortOrder; } }The sortExpression variable is obtained from the DataGrid and persisted in ViewState. Maybe I should package up my DataGrid so others can try it ...