Skip to content

A Quick Tip on SortCommand in DataGrid Tables

January 11, 2012

Here is a quick tip that I only realised now when using the SortCommand in DataGrid Tables, this is to don’t use the conventional sorting in the DataGrid Table, it doesn’t sort the columns properly at all, just link to the database source directly and store it into the DataTable as specified: –

protected void dgSessions_Sort(object source, DataGridSortCommandEventArgs e)
// Store the Sort Expression Direction

string sortExpressionDirection = “asc”;
if (SortExpressionDirection == null)
SortExpressionDirection = sortExpressionDirection;

//Retrieve the table from the session object.
DataTable dt = Session[“SessionTable”] as DataTable;
////dt.DefaultView.Sort = e.SortExpression + ” ” + SortOrder(SortExpressionDirection);

// Store the Sort Expression into session object.
Session[“SessionSortExpression”] = e.SortExpression;
Session[“SessionSortDirection”] = sortExpressionDirection;

tbl.DataSource = dt;

In the datatable code as specified: –

tbl.AllowSorting = true;
tbl.SortCommand += new DataGridSortCommandEventHandler(this.dgSessions_Sort);

You can see I commented out the dt.DefaultViewSort, simply store the ordered data in the Session[“SessionTable”] and it should work!


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: