Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I'm trying to have paging on a GridView so I've created Grid_View1_Paging method to handle this, however e.NewPageIndex always returns 1 hence the datagrid always stays on the 1st page.


This is my GridView:
ASP.NET
<asp:GridView ID="GridView1" runat="server" CellPadding="4"AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting" AllowPaging="true" OnPageIndexChanging="Grid_View1_Paging" EnableViewState="false">
         <Columns>
             <asp:BoundField DataField="GId" HeaderText="GId" SortExpression="GId"/>
             <asp:BoundField DataField="UTCCreated" HeaderText="Created" SortExpression="UTCCreated" />
             <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
          </Columns>




And this is my code behind:
C#
public DataTable dataTable = new DataTable("User");
protected void Page_Load(object sender, EventArgs e)
{
    GenerateHeaders();
    if (!Page.IsPostBack)
    {
        ClearUserForm();
        PopulateUserList();
    }
}

public void GenerateHeaders()
{
   //Add columns
    dataTable.Columns.Add("GId", typeof(int));
    dataTable.Columns.Add("UTCCreated", typeof(DateTime));
    dataTable.Columns.Add("UTCDeleted", typeof(DateTime));
    dataTable.Columns.Add("Name", typeof(string));
    dataTable.Columns.Add("ExternalID", typeof(string));
    dataTable.Columns.Add("UserTypeID", typeof(int));
    dataTable.Columns.Add("Deleted", typeof(bool));
}

public void PopulateUserList()
{
    GamificationEntities db = new GamificationEntities();

    List<User> user = db.SearchUserByName(txtSrchName.Text).ToList();

    // Add rows.
    foreach (var item in user)
    {
        object[] obj = new object[7];
        obj[0] = item.GId.ToString();
        obj[1] = item.UTCCreated;
        obj[2] = item.UTCDeleted;
        obj[3] = item.Name;
        obj[4] = item.ExternalID;
        obj[5] = item.UserTypeID;
        obj[6] = item.UTCDeleted;
        dataTable.Rows.Add(obj);
    }

    ddlUser.DataSource = user;
    ddlUser.DataValueField = "GId";
    ddlUser.DataTextField = "Name";
    ddlUser.DataBind();

    ddlUser.Items.Insert(0, new ListItem("--Add New--", "0"));
    GridView1.DataSource = dataTable;
    GridView1.DataBind();
}


protected void Grid_View1_Paging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;

    GridView1.DataSource = dataTable;
    GridView1.DataBind();
    PopulateUserList();
}
}


Could you help me out finding out the problem, please? I've tried multiple things by now and nothings seems to do the trick. I've added the code I think it's necessary to have an overview of the problem but if anything else needs to be added just tell me. I think the problem might be related to how I'm populating my GridView but didn't figured it out yet.

I'm new to this and this is my first question on codeproject so please bear with me please :)
Thanks in advance.

Cheers,
Luis
Posted
Comments
ZurdoDev 25-Aug-14 14:43pm    
Where are you databinding your grid? If in Page_Load make sure to check for IsPostBack.
[no name] 26-Aug-14 2:39am    
put ur gridview in ispostback on page load.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900