如何访问下一页中的GridView行? [英] How to access GridView rows in next Page?

查看:69
本文介绍了如何访问下一页中的GridView行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个网格视图,每页显示5行.如果用户需要,我也有一个按钮来添加额外的行.

我的问题是,当我将页码更改为当前页面到下一页时,我无法访问下一页中的行.实际上,我使用foreach()循环来访问网格中的所有行.但是,当我运行此循环时,它找不到第二页中的行索引"5".它会引发类似对象引用为NULL"的错误.

我的代码是...



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.
       Grid_Purchase.DataBind();
   }



我的fill()调用....

公共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 {
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();
dt.Rows.Add(dr);
}
return dt;
}

请给我您的建议.



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();
dt.Rows.Add(dr);
}
return dt;
}

Please give me your suggestions.

推荐答案

嘿. .

要访问其他页面中的行,您只需将tp设置为"allow pages = true"并设置页面索引..
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..



在页面加载中填充网格
问候,
shefeek
Hi,
fill the grid in page load
regards,
shefeek




请在页面加载时使用gridview绑定功能,然后在页面索引更改事件中使用.
希望它能起作用.


please use gridview bind function on page load and then use in page index changing event.
hope it works.


这篇关于如何访问下一页中的GridView行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆