.net – loop all gridview rows on button click when paging enabled

gridviewnet

I have paging enabled in my gridview…

I need this in button click event

  1. gridview total row count
  2. loop through all the rows in foreach

but it's only working for the current gridview page…help

protected void Button5_Click(object sender, EventArgs e)
{
    int[] no = new int[GridView2.Rows.Count];
    int i = 0;

    foreach (GridViewRow row in GridView2.Rows)
    {            
        Label l = (Label)row.FindControl("Label2");
        if (l.Text == "Unpaid")
        {
            int productID = Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Value);
            no[i] = productID;
            i++;
         }
     }
}

Best Answer

to achieve you have to disable paging before loop. rebind the grid & perform loop . Then enable paging & bind the grid again.

       protected void Button5_Click(object sender, EventArgs e)       
 { 
GridView2.AllowPaging = false;
 // do your databind here 
GridView2.databind(); 
int[] no = new int[GridView2.Rows.Count];      
          int i = 0;        
    foreach (GridViewRow row in GridView2.Rows)      
      {                             
   Label l = (Label)row.FindControl("Label2");
                    if (l.Text == "Unpaid")        
            {                    
    int productID = Convert.ToInt32(GridView2.DataKeys[row.RowIndex].Value);                        no[i] = productID;                        i++;                    }                }  


              )



GridView2.AllowPaging = true;
        // do your databind here again
        GridView2.databind();
Related Topic