hi,
try this
<asp:gridview xmlns:asp="#unknown">
ID="grdCause"
runat="server
EnableSortingAndPagingCallback="True"
AllowPaging="True"
AllowSorting="True"
onpageindexchanging="grdCause_PageIndexChanging"
onsorting="grdCause_Sorting"
>
</asp:gridview>
The following method is for Loading the Gridview .In this method am taking dataset and bind that dataset to Gridview.
ExecuteProcedure is the method in my sqlhelper class.For that method Click here.
private void LoadGrid()
{
DataSet ds = dal.ExecuteProcudere("CauseSelectAll", ht);
grdCause.DataSource = ds.Tables[0];
grdCause.DataBind();
}
Gridview pageindexChanging Event for paging:
protected void grdCause_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdCause.PageIndex = e.NewPageIndex;
LoadGrid();
}
Gridview Sorting Event for Sorting:
protected void grdCause_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
Use the following method for Sorting:
private void SortGridView(string sortExpression, string direction)
{
LoadGrid();
DataTable dt = grdCause.DataSource as DataTable;
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
grdCause.DataSource = dv;
grdCause.DataBind();
}
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value;
}
}