1,105,286 Community Members

Pagination

Member Avatar
M.Waqas Aslam
Practically a Posting Shark
837 posts since Aug 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

Hello All !

I want to make my gridview pagination look like this

First -prev -- 1 -2 -3 -4 -Next -Last

How can i do this , by using options of Mode it is not possible.

Best Regards

Member Avatar
geniusvishal
Junior Poster
183 posts since Jan 2012
Reputation Points: -1 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

You need to implement custom paging for your gridview.. This link should put some light on your requirement..
http://www.aspdotnet-suresh.com/2011/03/gridview-with-custom-paging.html

Member Avatar
ggamble
Junior Poster
151 posts since Mar 2006
Reputation Points: 1 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 4 [?]
 
0
 

Some code snips to get you going, and it includes sorting to. I'm creating a DataSet for the initial load but then creating a DataTable and storing it in ViewState for the paging. You could also, for efficiency, call only the page that is going to be shown, but thats a different solution. As long as your not pulling thousands of records you should be ok.

First add an event to Gridview:

        AllowSorting="True"
        OnSorting="gvProgramOptions_Sorting"
        OnPageIndexChanging="gridView_PageIndexChanging"

Then, in the method used to populate the DataSet, add this:

        ViewState["DataTable"] = null;
        DataTable dt = ds.Tables[0];
        ViewState["DataTable"] = dt;

These are the methods used to show the selected page:

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.DataSource = SortDataTable(ViewState["DataTable"] as DataTable, true);
            gridView.PageIndex = e.NewPageIndex;

            gridView.DataBind();
        }

        /// <summary>
        /// Sortable data table.
        /// </summary>
        /// <param name="dataTable">The data table.</param>
        /// <param name="isPageIndexChanging">if set to <c>true</c> [is page index changing].</param>
        /// <returns></returns>
        private DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
        {
            if (dataTable != null)
            {
                gridView.EditIndex = -1;
                DataView dataView = new DataView(dataTable);
                if (GridViewSortExpression != string.Empty)
                {
                    dataView.Sort = string.Format("{0} {1}", GridViewSortExpression,
                                                  isPageIndexChanging ? GridViewSortDirection : GetSortDirection());
                }

                ViewState["DataTable"] = dataView.ToTable();
                return dataView;
            }
            return new DataView();
        }            

        /// <summary>
        /// Handles the Sorting event of the gridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewSortEventArgs"/> instance containing the event data.</param>
        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            GridViewSortExpression = e.SortExpression;
            gridView.DataSource = SortDataTable(ViewState["DataTable"] as DataTable, false);
            gridView.DataBind();
            gridView.UseAccessibleHeader = true;
            gridView.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        /// <summary>
        /// Gets or sets the grid view sort direction.
        /// </summary>
        /// <value>
        /// The grid view sort direction.
        /// </value>
        private string GridViewSortDirection
        {
            get { return ViewState["SortDirection"] as string ?? "ASC"; }
            set { ViewState["SortDirection"] = value; }
        }

        private string GridViewSortExpression
        {
            get { return ViewState["SortExpression"] as string ?? string.Empty; }
            set { ViewState["SortExpression"] = value; }
        }

        /// <summary>
        /// Gets the sort direction.
        /// </summary>
        /// <returns>A sort direction</returns>
        private string GetSortDirection()
        {
            switch (GridViewSortDirection)
            {
                case "ASC":
                    GridViewSortDirection = "DESC";
                    break;
                case "DESC":
                    GridViewSortDirection = "ASC";
                    break;
            }
            return GridViewSortDirection;
        }
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: