5

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!

Painy James
  • 805
  • 2
  • 13
  • 26
user390480
  • 1,655
  • 4
  • 28
  • 61

1 Answers1

8

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();
    }
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I get, 'System.Web.UI.WebControls.DataGrid' does not contain a definition for 'PageIndex' – user390480 Mar 07 '11 at 20:29
  • @user390480 for DataGrid, try `dataGrid.CurrentPageIndex` instead – Bala R Mar 07 '11 at 20:32
  • OK, getting closer. I can go forward but not backwards. I don't have a PageIndexChanging event. I only have a PageIndexChanged. – user390480 Mar 07 '11 at 20:42
  • @user390480 PageIndexChanged should be okay. I tried with DataGrid instead of GridView and I can go both forward and backward. – Bala R Mar 07 '11 at 20:48
  • Well, for some reason PageIndexChanged isn't firing when I hit the < link. – user390480 Mar 07 '11 at 21:11
  • @user390480 weird. If possible, try my setup in a separate page and you will see it works both ways. – Bala R Mar 07 '11 at 21:12
  • I thought this was going to solve my problem, so I gave it a +1. Modified my code to include this, the `PageIndexChanging` event fired, and I was happy. Came back from lunch and did more debugging, only to find that I get **No records found** displayed when I try to go to the next page. –  Feb 20 '12 at 20:31