Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to read all rows of gridview when Paging is true;
Suppose pagesize is 2.
My gridview has 4 pages.
my code is
C#
 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();
}
Posted
Updated 15-Sep-15 23:52pm
v3
Comments
aarif moh shaikh 16-Sep-15 6:10am    
this code in wrong .. every time str1 and str2 will overlap.. check it
Member 7909353 16-Sep-15 6:43am    
I want to read only all rows irrespective of what values

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.
 
Share this answer
 
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);
}
 
Share this answer
 
Comments
Member 7909353 23-Sep-15 1:11am    
When I debug this line row = gvProducts.Rows[index];
Index out of range exception is occurred
Member 7909353 29-Sep-15 2:13am    
gvProducts.AllowPaging = false;
gvProducts.DataBind();
gvProducts.Rows.Count is zero.
gvProducts.Rows[index] index out of range exception is occurred

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