Gridview paging Show all Records

asp.netgridview

i have a gridview that has paging enabled. i also have a dropdown on the pager of the gridview where the user can select how many records per page they would like to retrieve. Once the dropdown is changed then an event is fired (shown below) to rerun the query with the updated results per page request. This works very well. I did however want to have a value "All" on the dropdown aswell and the method i used to impliment this is by disabling paging.

This all works brilliantly except for one issue. When the user selects "All" on the dropdown i would like to still show the pager once the gridview is updated. It doesnt show because i turned off paging but is there a way to show the pager again? See my code below for the event. (As you can see i renable the pager at the end but this has no effect)

thanks
damo

Code behind Event for Dropdown Change

void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //changes page size
            if ((((DropDownList)sender).SelectedValue).ToString() == "All")
            {

                GridViewMain.AllowPaging = false;

            }
            else
            {
                GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue);

            }



            //binds data source
            Result fAuditOverallStatusLatest = new Result(sConn);
            GridViewMain.DataSource = Result.getAuditOverallStatusLatest();            
            GridViewMain.PageIndex = 0;
            GridViewMain.DataBind();
            GridViewMain.AllowPaging = true;
            GridViewMain.BottomPagerRow.Visible = true;
            GridViewMain.TopPagerRow.Visible = true;
        }

DDL Code behind

protected void GridViewMain_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList GridViewMainddl = new DropDownList();
                //adds variants of pager size
                GridViewMainddl.Items.Add("5");
                GridViewMainddl.Items.Add("10");
                GridViewMainddl.Items.Add("20");
                GridViewMainddl.Items.Add("50");
                GridViewMainddl.Items.Add("100");
                GridViewMainddl.Items.Add("200");
                GridViewMainddl.Items.Add("500");
                GridViewMainddl.Items.Add("All");
                GridViewMainddl.AutoPostBack = true;
                //selects item due to the GridView current page size
                ListItem li = GridViewMainddl.Items.FindByText(GridViewMain.PageSize.ToString());
                if (li != null)
                    GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li);
                GridViewMainddl.SelectedIndexChanged += new EventHandler(GridViewMainddl_SelectedIndexChanged);
                //adds dropdownlist in the additional cell to the pager table
                Table pagerTable = e.Row.Cells[0].Controls[0] as Table;
                TableCell cell = new TableCell();
                cell.Style["padding-left"] = "15px";
                cell.Controls.Add(new LiteralControl("Page Size:"));
                cell.Controls.Add(GridViewMainddl);
                pagerTable.Rows[0].Cells.Add(cell);
                //add current Page of total page count
                TableCell cellPageNumber = new TableCell();
                cellPageNumber.Style["padding-left"] = "15px";
                cellPageNumber.Controls.Add(new LiteralControl("Page " + (GridViewMain.PageIndex + 1) + " of " + GridViewMain.PageCount));
                pagerTable.Rows[0].Cells.Add(cellPageNumber);

            }
        }

Best Answer

Put this in your Page_Init:

GridViewMain.PreRender += new EventHandler(GridViewMain_PreRender);

Then elsewhere in your Page class:

void GridViewMain_PreRender(object sender, EventArgs e)
{
    var pagerRow = (sender as GridView).BottomPagerRow;
    if (pagerRow != null)
    {
        pagerRow.Visible = true;
    }
}

Then for your drop down event:

void GridViewMainddl_SelectedIndexChanged(object sender, EventArgs e)
{
    MyServices fServices = new FAServices(sConn);
    Result fAuditOverallStatusLatest = new Result(sConn);
    var data = Result.getAuditOverallStatusLatest();

    //changes page size
    if ((((DropDownList)sender).SelectedValue).ToString() == "All")
    {
        GridViewMain.PageSize = data.Count();
    }
    else
    {
        GridViewMain.PageSize = int.Parse(((DropDownList)sender).SelectedValue);
    }

    //binds data source
    GridViewMain.DataSource = data;            
    GridViewMain.PageIndex = 0;
    GridViewMain.DataBind();
    GridViewMain.AllowPaging = true;
}

In that PreRender event, you'll have to duplicate that code for the top pager.

Edit: To get All selected, make this change in GridViewMain_RowCreated:

if (li != null)
{
    GridViewMainddl.SelectedIndex = GridViewMainddl.Items.IndexOf(li);
}
else
{
    GridViewMainddl.SelectedIndex = GridViewMainddl.Items.Count - 1;
}
Related Topic