SunVigna
SunVigna

Reputation: 77

Custom Paging in ASP.Net

I want to make custom paging in GridView in ASP.Net, my data source is a object data. I checked the web to find any code for this and found one which is not working for me.

  protected void invoiceReportsgridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (sender != null)
        {
            displayInvListgridView.PageIndex = e.NewPageIndex;
            displayInvListgridView.EditIndex = -1;
            displayInvListgridView.SelectedIndex = -1;
        }
    }

    protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            CustomizePageBar(e);
        }
    }

    void ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Add")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            GridViewRow row = displayInvListgridView.Rows[index];

            // Create a new ListItem object for the product in the row.     
            ListItem item = new ListItem();
            item.Text = Server.HtmlDecode(row.Cells[1].Text);

            // If the product is not already in the ListBox, add the ListItem 
            // object to the Items collection of the ListBox control. 
            //if (!displayInvListgridView.Items.Contains(item))
            //{
            //    displayInvListgridView.Items.Add(item);
            //}
        }
    }

    private void CustomizePageBar(GridViewRowEventArgs e)
    {

        //Table tblPager = new Table();
        tblPager.BorderWidth = 0;
        tblPager.CellPadding = 0;
        tblPager.CellSpacing = 0;
        tblPager.Width = Unit.Percentage(100);
        tblPager.Height = Unit.Pixel(20);

        //add a row for our pager contents
        tblPager.Rows.Add(new TableRow());

        //Spacer Cell
        TableCell tcelSpace = new TableCell();
        tcelSpace.Width = Unit.Pixel(360);


        //Page x of y Cell
        TableCell tcelXofY = new TableCell();
        tcelXofY.Width = Unit.Pixel(100);

        Label litXofY = new Label();
        litXofY.Text = "Page " + (displayInvListgridView.PageIndex + 1) + " of " + displayInvListgridView.PageCount;
        litXofY.Font.Bold = true;
        tcelXofY.Controls.Add(litXofY);


        //lable GoTo
        Label lblGoto = new Label();
        lblGoto.Text = "GoTo: ";
        lblGoto.ID = "lblGoTo";
        lblGoto.Font.Bold = true;
        lblGoto.Font.Size = displayInvListgridView.PagerStyle.Font.Size;

        TableCell tcelGoto = new TableCell();
        tcelGoto.Width = Unit.Pixel(25);
        tcelGoto.Controls.Add(lblGoto);


        //Pick drop downlist
        TableCell tcelPickPage = new TableCell();
        tcelPickPage.Width = Unit.Pixel(25);
        //The dropdown list box
        DropDownList ddlPickPage = new DropDownList();
        ddlPickPage.ID = "ddlPick";
        ddlPickPage.AutoPostBack = true;
        ddlPickPage.EnableViewState = true;
        ddlPickPage.Font.Size = displayInvListgridView.PagerStyle.Font.Size;

        for (int index = 1; index <= displayInvListgridView.PageCount; index++)
        {
            ddlPickPage.Items.Add(index.ToString());
        }
        ddlPickPage.SelectedIndex = displayInvListgridView.PageIndex;

        //handle event for picklist
        ddlPickPage.SelectedIndexChanged += new EventHandler(OnPagePicked);
        tcelPickPage.Controls.Add(ddlPickPage);

        //The existing Nav controls
        TableCell tcelNav = new TableCell();
        tcelNav.Width = Unit.Pixel(150);

        //for move all existing controls
        foreach (Control ctrl in e.Row.Cells[0].Controls)
        {
            tcelNav.Controls.Add(ctrl);
        }

        // add all cells to new pager

        tblPager.Rows[0].Cells.Add(tcelSpace);
        tblPager.Rows[0].Cells.Add(tcelXofY);
        tblPager.Rows[0].Cells.Add(tcelGoto);
        tblPager.Rows[0].Cells.Add(tcelPickPage);
        tblPager.Rows[0].Cells.Add(tcelNav);


        //replace grids pager with new
        e.Row.Cells[0].Controls.Add(tblPager);

    }


    protected void OnPagePicked(object sender, EventArgs e)
    {
        DropDownList ddlPick = (DropDownList)sender;
        displayInvListgridView.PageIndex = Convert.ToInt32(ddlPick.SelectedItem.Value) - 1;
        //Raise page index changed so user can rebind data

        GridViewPageEventArgs gvArgs = new GridViewPageEventArgs(Convert.ToInt32(ddlPick.SelectedItem.Value) - 1);
        //OnPageIndexChanging(gvArgs);
        GridViewPageEventArgs ex = new GridViewPageEventArgs(Convert.ToInt32(ddlPick.SelectedItem.Value) - 1);
        invoiceReportsgridView_PageIndexChanging(sender, gvArgs);
    }

Upvotes: 0

Views: 1884

Answers (1)

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17380

The idea behind the manual Gridview paging is that you need to rebind the gridview on each page change, and also you need to change the page index. Here is a simple example:

//This is the method to bind the gridview to the data.
private void LoadGridview()
{
    List<MyObject> MyObjectList = MyObject.FillMyList();
    if (MyObjectList != null && MyObjectList.Count > 0)
    {
        this.GridView1.DataSource = MyObjectList;
        this.GridView1.DataBind();
    }
}

//When the index changes
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Changes the page
    this.GridView1.PageIndex = e.NewPageIndex;
    LoadGridview();
}
protected void Page_Load(object sender, EventArgs e)
{
    //When the page loads
    LoadGridview();
}

Good luck!

Upvotes: 1

Related Questions