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: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:
public DataTable dataTable = new DataTable("User");
protected void Page_Load(object sender, EventArgs e)
{
GenerateHeaders();
if (!Page.IsPostBack)
{
ClearUserForm();
PopulateUserList();
}
}
public void GenerateHeaders()
{
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();
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