Recent Posts

Posts RSS

How to do only Ascending/Descending Sort in Telerik RadGrid

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!

kick it on DotNetKicks.com

0 comments: