Tuesday, January 27, 2015

how to do sorting on gridview


 private void BindGrid()
        {
            if (ViewState["SortExp"] != null && ViewState["order"] != null)
            {
                lstemp.Sort(new GenericComparer<Employee>(ViewState["SortExp"].ToString(), (SortDirection)ViewState["order"]));
            }
            GridView1.DataSource = lstemp;
            GridView1.DataBind();

        }
        SortDirection sortOrder;
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (ViewState["SortExp"] != null && ViewState["SortExp"].ToString().ToLower() == e.SortExpression.ToString().ToLower()
                        && ViewState["order"] != null && (SortDirection)ViewState["order"] == SortDirection.Ascending)
                {
                    sortOrder = SortDirection.Descending;
                }
                else
                {
                    sortOrder = SortDirection.Ascending;
                }

                ViewState["order"] = sortOrder;
                ViewState["SortExp"] = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
               
            }
        }

create new class

public class GenericComparer<T> : IComparer<T>
    {
        /// <summary>
        /// Define sort direction Ascending or descending
        /// </summary>
        private SortDirection sortDirection;

        /// <summary>
        /// Gets or sets the sort direction.
        /// </summary>
        /// <value>The sort direction.</value>
        /// <remarks></remarks>
        public SortDirection SortDirection
        {
            get { return this.sortDirection; }
            set { this.sortDirection = value; }
        }

        /// <summary>
        /// Define the name in which data is to be sort.
        /// </summary>
        private string sortExpression;

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericComparer&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="sortExpression">The sort expression.</param>
        /// <param name="sortDirection">The sort direction.</param>
        /// <remarks></remarks>
        public GenericComparer(string sortExpression, SortDirection sortDirection)
        {
            this.sortExpression = sortExpression;
            this.sortDirection = sortDirection;
        }
        public int Compare(T x, T y)
        {
            PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);
            IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
            IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);

            if (obj1 == null)
            {
                if (SortDirection == SortDirection.Ascending)
                {
                    return (obj2 == null) ? 0 : -1;
                }
                else
                {
                    return (obj2 == null) ? -1 : 0;
                }
            }
            if (obj2 == null) { return 1; }

            if (SortDirection == SortDirection.Ascending)
            {
                return obj1.CompareTo(obj2);
            }
            else return obj2.CompareTo(obj1);
        }
     
    }

0 comments:

Post a Comment

Followers