Prob with paging
-
In my asp.net project i use arraylist to add data to my datagrid.first i add my text box values to arraylist and then i set my arraylist to datagrid as a datasource.my code worked when i disable the paging of the datagrid. But when i enable the paging it gives error. Do I need to convert my array list into a dataset or dataview. Can any one provide me better solution? Thanks
-
In my asp.net project i use arraylist to add data to my datagrid.first i add my text box values to arraylist and then i set my arraylist to datagrid as a datasource.my code worked when i disable the paging of the datagrid. But when i enable the paging it gives error. Do I need to convert my array list into a dataset or dataview. Can any one provide me better solution? Thanks
You basically can use ArrayList as a datasource of the DataGrid control. You can take a quick look at the BaseDataList.DataSource property[^] to see which object can be used as a datasource for the control. Could you be more specific about your error? And could you post a snippet of your sample code? Then we can try to help you figure out your problem here.
-
You basically can use ArrayList as a datasource of the DataGrid control. You can take a quick look at the BaseDataList.DataSource property[^] to see which object can be used as a datasource for the control. Could you be more specific about your error? And could you post a snippet of your sample code? Then we can try to help you figure out your problem here.
Dear Mr.Minhpc thanks for the reply.any way this is the code which i use to add data to my grid. private void btnTaskAdd_Click(object sender, System.EventArgs e) { EPGTask = new EPGProjectTask(); EPGTask.EPGTProjID = txtProjID.Text.ToUpper(); EPGTask.EPGTPhaseID = txtPhaseID.Text.ToUpper(); EPGTask.EPGTPhaseName = txtPhaseDesc.Text.ToUpper(); EPGTask.EPGTTaskID = txtTaskID.Text.ToUpper(); EPGTask.EPGTDesc = txtTaskDesc.Text.ToUpper(); lstTask.Add(EPGTask); bindTaskDetails(); } private void bindTaskDetails() { foreach(DataGridItem item in DGTask.Items) { EPGTask = new EPGProjectTask(); //read the item template if(!item.ItemType.ToString().Equals("EditItem")) { EPGTask.EPGTProjID = ((Label)item.Cells[1].FindControl("lbl1")).Text; EPGTask.EPGTPhaseID= ((Label)item.Cells[2].FindControl("lbl2")).Text; EPGTask.EPGTPhaseName= ((Label)item.Cells[3].FindControl("lbl3")).Text; EPGTask.EPGTTaskID= ((Label)item.Cells[4].FindControl("lbl4")).Text; EPGTask.EPGTDesc= ((Label)item.Cells[5].FindControl("lbl5")).Text; } //read the edit item template else if(item.ItemType.ToString().Equals("EditItem")) { EPGTask.EPGTProjID= ((TextBox)item.Cells[1].FindControl("txt1")).Text; EPGTask.EPGTPhaseID= ((TextBox)item.Cells[2].FindControl("txt2")).Text; EPGTask.EPGTPhaseName = ((TextBox)item.Cells[3].FindControl("txt3")).Text; EPGTask.EPGTTaskID = ((TextBox)item.Cells[4].FindControl("txt4")).Text; EPGTask.EPGTDesc = ((TextBox)item.Cells[5].FindControl("txt5")).Text; } lstTask.Add(EPGTask); } DGTask.DataSource = lstTask; DGTask.DataBind(); } private void DGTask_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DGTask.CurrentPageIndex = e.NewPageIndex; }
-
Dear Mr.Minhpc thanks for the reply.any way this is the code which i use to add data to my grid. private void btnTaskAdd_Click(object sender, System.EventArgs e) { EPGTask = new EPGProjectTask(); EPGTask.EPGTProjID = txtProjID.Text.ToUpper(); EPGTask.EPGTPhaseID = txtPhaseID.Text.ToUpper(); EPGTask.EPGTPhaseName = txtPhaseDesc.Text.ToUpper(); EPGTask.EPGTTaskID = txtTaskID.Text.ToUpper(); EPGTask.EPGTDesc = txtTaskDesc.Text.ToUpper(); lstTask.Add(EPGTask); bindTaskDetails(); } private void bindTaskDetails() { foreach(DataGridItem item in DGTask.Items) { EPGTask = new EPGProjectTask(); //read the item template if(!item.ItemType.ToString().Equals("EditItem")) { EPGTask.EPGTProjID = ((Label)item.Cells[1].FindControl("lbl1")).Text; EPGTask.EPGTPhaseID= ((Label)item.Cells[2].FindControl("lbl2")).Text; EPGTask.EPGTPhaseName= ((Label)item.Cells[3].FindControl("lbl3")).Text; EPGTask.EPGTTaskID= ((Label)item.Cells[4].FindControl("lbl4")).Text; EPGTask.EPGTDesc= ((Label)item.Cells[5].FindControl("lbl5")).Text; } //read the edit item template else if(item.ItemType.ToString().Equals("EditItem")) { EPGTask.EPGTProjID= ((TextBox)item.Cells[1].FindControl("txt1")).Text; EPGTask.EPGTPhaseID= ((TextBox)item.Cells[2].FindControl("txt2")).Text; EPGTask.EPGTPhaseName = ((TextBox)item.Cells[3].FindControl("txt3")).Text; EPGTask.EPGTTaskID = ((TextBox)item.Cells[4].FindControl("txt4")).Text; EPGTask.EPGTDesc = ((TextBox)item.Cells[5].FindControl("txt5")).Text; } lstTask.Add(EPGTask); } DGTask.DataSource = lstTask; DGTask.DataBind(); } private void DGTask_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DGTask.CurrentPageIndex = e.NewPageIndex; }
There are a couple of things come to mind: + You basically need to do two things in the event handler of the
PageIndexChanged
event of the DataGrid control:private void DGTask_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
//You first set the CurrentPageIndex property to the selected index.
DGTask.CurrentPageIndex = e.NewPageIndex;//Then rebind the datasource to the DataGrid control to refresh the control. DGTask.DataSource = lstTask; DGTask.DataBind();
}
For more information, see DataGrid.PageIndexChanged Event [^] + The way you populate the
lstTask
instance in thebindTaskDetails
method looks strange to me, why not just save thelstTask
object inSession
then you can use it later when you want to get/add items of the arraylist. -
There are a couple of things come to mind: + You basically need to do two things in the event handler of the
PageIndexChanged
event of the DataGrid control:private void DGTask_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
//You first set the CurrentPageIndex property to the selected index.
DGTask.CurrentPageIndex = e.NewPageIndex;//Then rebind the datasource to the DataGrid control to refresh the control. DGTask.DataSource = lstTask; DGTask.DataBind();
}
For more information, see DataGrid.PageIndexChanged Event [^] + The way you populate the
lstTask
instance in thebindTaskDetails
method looks strange to me, why not just save thelstTask
object inSession
then you can use it later when you want to get/add items of the arraylist.Dear Mr.Minhpc thanks for the solution.but still i have same problem.i have added my arraylist to a session. This is the code which is in my bind method.... lstTask.Add(EPGTask); Session["TaskList"] = lstTask; ArrayList dd = new ArrayList(); dd = (ArrayList)Session["TaskList"]; DGTask.DataSource= dd; DGTask.DataBind(); this is in the pageindexpagechange..... DGTask.CurrentPageIndex = e.NewPageIndex; ArrayList dd = new ArrayList(); dd = (ArrayList)Session["TaskList"]; DGTask.DataSource= dd; DGTask.DataBind(); but when i add elements to grid,every time it shows only 1 element in the second page.and also i go to second page of my grid and try to add element then it gives error called ... Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount Please provide me better solution.
-
Dear Mr.Minhpc thanks for the solution.but still i have same problem.i have added my arraylist to a session. This is the code which is in my bind method.... lstTask.Add(EPGTask); Session["TaskList"] = lstTask; ArrayList dd = new ArrayList(); dd = (ArrayList)Session["TaskList"]; DGTask.DataSource= dd; DGTask.DataBind(); this is in the pageindexpagechange..... DGTask.CurrentPageIndex = e.NewPageIndex; ArrayList dd = new ArrayList(); dd = (ArrayList)Session["TaskList"]; DGTask.DataSource= dd; DGTask.DataBind(); but when i add elements to grid,every time it shows only 1 element in the second page.and also i go to second page of my grid and try to add element then it gives error called ... Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount Please provide me better solution.
Can you run your application in debug mode and try to see the values of the
CurrentPageIndex
andPageCount
properties before the error happens? I doubt that those values are not inline with your datasource (the arraylist instance). If you just use a single web page for both listing all items and adding a new item, then you'll have to rebind the datasource to the datagrid so that it can reflect the updated datasource.