Click here to Skip to main content
15,064,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have an grid view displaying 5 rows per page. Also i have an button to add an extra row, if user wants.

My problem is While i change the page number to current page to next page i can't access the row in next page. Actually i use foreach() loop to access all rows in the grid. but while i run this loop it can't find the row index "5" which was in second page. It throws an error like "Object reference NULL".

My code is...

protected void Grid_Purchase_PageIndexChanging(object sender, GridViewPageEventArgs e)
       Grid_Purchase.PageIndex = e.NewPageIndex;
       Grid_Purchase.DataSource = Fill(); // Here i fill all rows into grid, error comes from here.

My fill() call....

public DataTable Fill()
DataTable dt = new DataTable("items");
dt.Columns.Add("sno", typeof(int));
dt.Columns.Add("Prod_id", typeof(string));
dt.Columns.Add("Prod_name", typeof(string));
dt.Columns.Add("Prod_size", typeof(string));
dt.Columns.Add("Prod_qty", typeof(string));
dt.Columns.Add("Prod_Per_amt", typeof(string));
dt.Columns.Add("Prod_amt", typeof(string));

for (int i = 0; i < totalnumberofrowsin_Grid;i++ )
DataRow dr = dt.NewRow();
System.Web.UI.WebControls.TextBox txt_Prodid = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_Pid"));
System.Web.UI.WebControls.DropDownList txt_Prodname = (System.Web.UI.WebControls.DropDownList)(Grid_Purchase.Rows[i].FindControl("txt_grd_ProdName"));
System.Web.UI.WebControls.TextBox txt_Prodsize = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_size"));
System.Web.UI.WebControls.TextBox txt_Prodqty = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_qty"));
System.Web.UI.WebControls.TextBox txt_Prodperamt = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_piece_amt"));
System.Web.UI.WebControls.TextBox txt_Prodamt = (System.Web.UI.WebControls.TextBox)(Grid_Purchase.Rows[i].FindControl("txt_grd_amt"));
dr[0] = i + 1;
dr[1] = txt_Prodid.Text.ToString();
dr[2] = txt_Prodname.Text.ToString();
dr[3] = txt_Prodsize.Text.ToString();
dr[4] = txt_Prodqty.Text.ToString();
dr[5] = txt_Prodperamt.Text.ToString();
dr[6] = txt_Prodamt.Text.ToString();
return dt;

Please give me your suggestions.
Updated 6-Jul-11 20:31pm
[no name] 7-Jul-11 2:17am
show full code where and how u use foreach loop etc...
J.Karthick 7-Jul-11 2:31am
Check i'm update my fill()

hey man..Whats the need of binding the gridview inside PageIndexChanging event...Bind it in page load or other events...

For accessing rows in other page you only hv tp set "allow paging=true" and set the page index..
J.Karthick 7-Jul-11 2:29am
I dont want to fill my grid? Then how can i change my page? Please explain...Am newbie
Mathews Davis 7-Jul-11 2:41am
Ha...It doesnt matter man..Jus fill the grid and set paging to true and also set the no: of rows you want to see on one page..thats all..Y so complications!!!!!!!!
J.Karthick 7-Jul-11 2:55am
Without pageindexchanging event its not change my page....its remains on same page but page number alone came at the bottom.
fill the grid in page load
J.Karthick 7-Jul-11 3:04am
ya...i filled by condition

please use gridview bind function on page load and then use in page index changing event.
hope it works.
J.Karthick 7-Jul-11 3:05am
ya...i used it with postback condition...but no luck

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