Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView from CodeBehind in C# Paging not working

when I click the > on my GridView it does not go to the next set of records.

        DataGrid dataGrid = new DataGrid();
        dataGrid.PageSize = 5;
        dataGrid.AllowPaging = true;
        dataGrid.EnableViewState = true;
        dataGrid.DataSource = customerDataTable;
        dataGrid.AllowPaging ();
        if (!IsPostBack)
        {
            dataGrid.DataBind();
        }

Depending on my code, it either stays on the first 5 or the grid does not show.

I've tried the DataBind() in and out of the IsPostBack.

I've also tried adding

        dataGrid.PageIndexChanged += new DataGridPageChangedEventHandler(dataGrid_PageIndexChanged);

and

    void dataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        DataGrid dg = (DataGrid)source;
        dg.DataBind();
    }

But I can't get this to work. What am I doing wrong?

Thanks!

like image 811
user390480 Avatar asked Mar 07 '11 20:03

user390480


1 Answers

Here's an example I tried to recreate your scenario and it works. Check it out.

protected void Page_Load(object sender, EventArgs e)
{
        GridView GridView1 = new GridView();
        Panel1.Controls.Add(GridView1);
        GridView1.DataSource = GetList();
        GridView1.AutoGenerateColumns = true;
        GridView1.EnableViewState = true;
        GridView1.AllowPaging = true;
        GridView1.PageSize = 4;
        GridView1.DataBind();
        GridView1.PageIndexChanging += new GridViewPageEventHandler(GridView1_PageIndexChanging);

}

void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    if (sender != null)
    {
        GridView GridView1 = sender as GridView;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}



public class Person
{
    public String Name { get; set; }
    public int Age { get; set; }
}
private IEnumerable<Person> GetList()
{
    List<Person> list = new List<Person>();
    list.Add(new Person() {Age = 12, Name = "asdfsd"});
    list.Add(new Person() {Age = 13, Name = "sdfsdaf"});
    list.Add(new Person() {Age = 14, Name = "zxczxv"});
    list.Add(new Person() { Age = 15, Name = "zxczxv" });
    list.Add(new Person() { Age = 16, Name = "zxczxv" });
    list.Add(new Person() { Age = 17, Name = "zxczxv" });
    return list;
}

and in the markup all you need is to have the panel

<asp:Panel ID="Panel1" runat="server">

EDIT:

Here's the same scenario using DataGrid

protected void Page_Load(object sender, EventArgs e)
{
        DataGrid dataGrid = new DataGrid();
        Panel1.Controls.Add(dataGrid);
        dataGrid.DataSource = GetList();
        dataGrid.AutoGenerateColumns = true;
        dataGrid.EnableViewState = true;
        dataGrid.AllowPaging = true;
        dataGrid.PageSize = 4;
        dataGrid.DataBind();
        dataGrid.PageIndexChanged +=new DataGridPageChangedEventHandler(dataGrid_PageIndexChanged);

}

void dataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
    if (source != null)
    {
        DataGrid dataGrid = source as DataGrid;
        dataGrid.CurrentPageIndex = e.NewPageIndex;
        dataGrid.DataBind();
    }
}
like image 86
Bala R Avatar answered Nov 15 '22 03:11

Bala R