For adding the columnwise sorting functionality in grid, we will modify columnheader row of the GridView.
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].Text = string.Format("{0}<img alt=\"Ascending\" src=\"Images/up.jpg\" onclick=\"UpdateGrid('sort${0} Asc')\"; /><img alt=\"Descending\" src=\"Images/down.jpg\" onclick=\"UpdateGrid('sort${0} desc') \"; />", e.Row.Cells[i].Text);
}
We have added up and down images and an onclick event of either of them will call the UpdateGrid function (Ref. Point 4) for sorting Ascending and Descending order repectively. [At this point you can complie the code and the grid will be sortable].
After this we will modify the Pager row in such a way that we can use it to change the page index of the grid using UpdateGrid function.(Ref. point 4 public void RaiseCallbackEvent)
else if (e.Row.RowType == DataControlRowType.Pager)
GridView gdv = (GridView)sender;
int _pageCount = gdv.PageCount;
e.Row.Cells[0].Text = "";
for (int i = 0; i < _pageCount; i++)
HyperLink hyp = new HyperLink();
hyp.Text = i.ToString() + " ";
hyp.Attributes.Add("href", "javascript:UpdateGrid('changePage$" + i + "');");
e.Row.Cells[0].Controls.Add(hyp);
Label l = new Label();
l.Text = " ";
e.Row.Cells[0].Controls.Add(l);
hyp = null;
_pageCount contains the pagecount of the GridView. We have to display pages as Numbers so we used a HyperLink and a Label to add space after each page number.
Finally, we can set EnableViewState="false" this will not affect the functionality but the extra code of the ViewState will not come on client side which leads to lighter page.
The entire code for the sample can be downloaded here