启用分页时,在按钮单击时循环所有gridview行

时间:2009-11-10 08:48:54

标签: .net gridview

我在gridview中启用了分页...

我在按钮点击事件中需要这个

  1. gridview总行数
  2. 遍历foreach中的所有行
  3. 但它仅适用于当前的gridview页面... 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++;
             }
         }
    }
    

1 个答案:

答案 0 :(得分:3)

实现你必须在循环之前禁用分页。重新绑定网格&执行循环。 然后启用分页&再次绑定网格。

       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();