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

Recommended Answers

All 2 Replies

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;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.