Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 13 June 2014

GridView Sorting Sample Programmatically

 protected void gv_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortingDirection = string.Empty;
            DataTable dtReg = (DataTable)ViewState["gv"];

            if (direction == SortDirection.Ascending)
            {
                direction = SortDirection.Descending;
                sortingDirection = "Desc";
            }
            else
            {
                direction = SortDirection.Ascending;
                sortingDirection = "Asc";
            }
            DataView sortedView = new DataView(dtReg);
            sortedView.Sort = e.SortExpression + " " + sortingDirection;
            Session["objects"] = sortedView;
            gv.DataSource = sortedView;
            gv.DataBind();

        }
public SortDirection direction
        {
            get
            {
                if (ViewState["directionState"] == null)
                {
                    ViewState["directionState"] = SortDirection.Descending;
                }
                return (SortDirection)ViewState["directionState"];
            }
            set
            { ViewState["directionState"] = value; }
        }

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv.PageIndex = e.NewPageIndex;
            gv.DataSource = Session["objects"];
            gv.DataBind();
        }

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.