开发者

GridView from CodeBehind in C# Paging not working

开发者 https://www.devze.com 2023-02-15 04:07 出处:网络
when I click the > on my Gr开发者_如何学JAVAidView it does not go to the next set of records. DataGrid dataGrid = new DataGrid();

when I click the > on my Gr开发者_如何学JAVAidView 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!


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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消