如何在分页为真时读取gridview的所有行 [英] how to read all rows of gridview when paging is true

查看:57
本文介绍了如何在分页为真时读取gridview的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在Paging为true时读取gridview的所有行;

假设pagesize为2.

我的gridview有4页。

我的代码是

I want to read all rows of gridview when Paging is true;
Suppose pagesize is 2.
My gridview has 4 pages.
my code is

 foreach (GridViewRow rows in LeftPolicy.Rows)
            {
               
string str1=((HiddenField)rows.Cells[1].FindControl("str1")).Value.ToString();
string str2=((HiddenField)rows.Cells[1].FindControl("str2")).Value.ToString();
}

推荐答案

如果你有分页,那么gridview只保存可见的数据,即当前页面,你无法获得其他行。如果您可以访问当前页面之外的数据取决于您获得的数据以及您的分页方式。如果获得所有数据,则将gridview绑定到该数据的单个页面,然后您可以从绑定gridview的数据中访问所有行。如果您只从数据源获取当前页面,那就是gridview中显示的内容,那么您根本无法获取当前页面之外的数据,因为您从未检索过它。要访问数据,您需要从任何地方检索它。
If you have paging then the gridview only holds the data that is visible, ie the current page, you can't get other rows. If you can access data outside the current page depends on what data you are getting and how you are doing your paging. If you get all the data then bind the gridview to a single page of that data then you can access all the rows from the data that you bound the gridview from. If you only get the current page from your datasource and that is what is shown in the gridview then you simply can't get at data outside the current page as you have never retrieved it. To access the data you'll need to retrieve it from wherever it is kept.


protected void btnDisplay_Click(object sender,EventArgs e)

{

gvProducts.AllowPaging = false;

gvProducts.DataBind();

int rowCount = Convert.ToInt32(tbxRowCount.Text);

GridViewRow row = null;

for(int index = 0; index< rowCount; index ++)

{

row = gvProducts .Rows [index];

Label lblProductName =(Label)row.FindControl(lblProductName);

Label lblProductNumber =(Label)row.FindControl(lblProductNumber );

标签lblProductPrice =(标签)row.FindControl(lblProductPrice);

AddRow(lblProductName.Text,lblProductNumber.Text,

lblProductPrice.Text);

}

gvProducts.AllowPaging = true;

gvProducts.DataBind();

}

private void AddRow(string productName,string productNumber,string price)

{

HtmlTableRow row = new HtmlTableRow();

HtmlTableCell productNameCell = new HtmlTableCell();

HtmlTableCell productNumberCell = new HtmlTableCell();

HtmlTableCell productPriceCell = new HtmlTableCell();

productNameCell.InnerText = productName;

productNumberCell.InnerText = productNumber;

productPriceCell.InnerText = String.Format({0:C},price);

row .Cells.Add(productNameCell);

row.Cells.Add(productNumberCell);

row.Cells.Add(productPriceCell);

tblProducts.Rows.Add(row);

}
protected void btnDisplay_Click(object sender, EventArgs e)
{
gvProducts.AllowPaging = false;
gvProducts.DataBind();
int rowCount = Convert.ToInt32(tbxRowCount.Text);
GridViewRow row = null;
for (int index = 0; index < rowCount; index++)
{
row = gvProducts.Rows[index];
Label lblProductName = (Label)row.FindControl("lblProductName");
Label lblProductNumber = (Label)row.FindControl("lblProductNumber");
Label lblProductPrice = (Label)row.FindControl("lblProductPrice");
AddRow(lblProductName.Text, lblProductNumber.Text,
lblProductPrice.Text);
}
gvProducts.AllowPaging = true;
gvProducts.DataBind();
}
private void AddRow(string productName, string productNumber, string price)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell productNameCell = new HtmlTableCell();
HtmlTableCell productNumberCell = new HtmlTableCell();
HtmlTableCell productPriceCell = new HtmlTableCell();
productNameCell.InnerText = productName;
productNumberCell.InnerText = productNumber;
productPriceCell.InnerText = String.Format("{0:C}", price);
row.Cells.Add(productNameCell);
row.Cells.Add(productNumberCell);
row.Cells.Add(productPriceCell);
tblProducts.Rows.Add(row);
}


这篇关于如何在分页为真时读取gridview的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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