Many times sorting a datagrid involves paging, which will mean that you will need execute another query when a different sort column is chosen. I would recommend that you setup an event handler for the SortCommand to handle setting the new sort column and the direction.
I would also setup a method that fetches the data for your grid.
You should maintain the current sort column along with the direction in ViewState or Session.
Here's a snippet of code that I have in C# that does this.
public partial class _Default : System.Web.UI.Page
{
private string SortColumn
{
get { return ViewState["SortColumn"].ToString(); }
set { ViewState["SortColumn"] = value; }
}
private bool Ascending
{
get { return (bool)ViewState["Ascending"]; }
set { ViewState["Ascending"] = value; }
}
public int PageIndex
{
get { return (int)ViewState["PageIndex"]; }
set { ViewState["PageIndex"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindData();
}
private void mAwardees_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
if (this.SortColumn == e.SortExpression)
this.Ascending = !this.Ascending;
else
this.Ascending = true;
SortColumn = e.SortExpression;
BindData();
}
public void BindData()
{
mAwardees.DataSource = AwardeeStatusSearch.Search(SortColumn, Ascending, PageIndex + 1, mAwardees.PageSize);
mAwardees.CurrentPageIndex = PageIndex;
mAwardees.VirtualItemCount = AppContext.Current.AwardeeStatusSearch.RecordCount;
}
}