How to do only Ascending/Descending Sort in Telerik RadGrid
Posted by
Manas
at
4:42:00 PM
Reactions:
For sorting you have to first declare the event name in page design code like this:
OnSortCommand="radGridView_OnSorting"
Then add the following code snippets in server side code as follows:
C# code:
public GridSortOrder GridViewSortDirection
{
get
{
if (Session["SortDirection"] == null)
{
Session["SortDirection"] = GridSortOrder.Ascending;
}
return (GridSortOrder)Session["SortDirection"];
}
set
{
Session["SortDirection"] = value;
}
}
protected void radGridView_OnSorting(object sender, GridSortCommandEventArgs e)
{
if (e.SortExpression.Equals(ActiveSortExpression) && GridViewSortDirection == GridSortOrder.Ascending)
{
this.GridViewSortDirection = GridSortOrder.Ascending;
}
else if (e.SortExpression.Equals(ActiveSortExpression) && GridViewSortDirection == GridSortOrder.Descending)
{
this.GridViewSortDirection = GridSortOrder.Descending;
}
else
{
this.GridViewSortDirection = GridSortOrder.Ascending;
}
this.ActiveSortExpression = e.SortExpression;
}
And then add following codes to your BindGrid() method where you bind the GridView:
private void BindGrid()
{
radGridView.MasterTableView.AllowNaturalSort = false;
string sortExpression = this.ActiveSortExpression;
if (this.GridViewSortDirection == GridSortOrder.Ascending)
{
sortExpression += " ASC";
}
else
{
sortExpression += " DESC";
}
radGridView.Sort = sortExpression;
}
Hope It Helps!
Labels: Advanced, Asp.net C#
Subscribe to:
Post Comments (Atom)



0 comments:
Post a Comment