Thursday, February 18, 2010

GridView sorting/Paging

OnSorting="gvName_Sorting"
OnPageIndexChanging="gvName_PageIndexChanging"

protected void gvName_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvName.PageIndex = e.NewPageIndex;
BindEvents();
}


protected void gvName_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewSortExpression = e.SortExpression;
SetSortDirection();
BindEvents();
}

protected void BindEvents()
{
DataView dvName = new DataView();

dvName.Sort = GridViewSortExpression + " " + GridViewSortDirection;
dvName.DataSource = dvEvents;
dvName.DataBind();
}

private string GridViewSortDirection
{
get { return ViewState["SortDirection"] as string ?? "DESC"; }
set { ViewState["SortDirection"] = value; }
}

private string GridViewSortExpression
{
get { return ViewState["SortExpression"] as string ?? "DEFAULT SORT COLUMN"; }
set { ViewState["SortExpression"] = value; }
}

private void SetSortDirection()
{
GridViewSortDirection = (GridViewSortDirection == "DESC") ? "ASC" : "DESC";
}

No comments: