在GridView1_RowDataBound 事件里面if (e.Row.RowType == DataControlRowType.DataRow)      
 {
            int i = e.Row.RowIndex; //从0开始
            Label lbl_bh = (Label)e.Row.Cells[i].FindControl("lblbh");
            Label lbl_name = (Label)e.Row.Cells[i].FindControl("lblname");
  }
当GridView1里有5条数据时候没有任何问题;
但是加到第6条数据时候
i = 5
Label lbl_bh = (Label)e.Row.Cells[i].FindControl("lblbh");
发现 lbl_bh 为null;
报:指定的参数已超出有效值的范围。参数名: index明明是有6条数据的,1到5条数据就没报错.... 挺郁闷的
谁可以帮帮忙??? 

解决方案 »

  1.   

         int i = e.Row.RowIndex; //从0开始 ===>
    这个i是row的索引。
    Cells[i].这个i是列的索引,这个Cells.Count是固定的。你数据再多,也就那么几列。
      

  2.   

                int i = e.Row.RowIndex; //从0开始    这里是行吧
                Label lbl_bh = (Label)e.Row.Cells[i].FindControl("lblbh");  这里i是列吧
                Label lbl_name = (Label)e.Row.Cells[i].FindControl("lblname");  这里i是列吧
      

  3.   

    cells[i],这个i指的是第几列,和你有几条数据无关,数据量是行数
      

  4.   

                Label lbl_bh = (Label)e.Row.Cells[i].FindControl("lblbh"); 
                Label lbl_name = (Label)e.Row.Cells[i].FindControl("lblname"); =========》》》》            Label lbl_bh = (Label)e.Row.FindControl("lblbh"); 
              Label lbl_name = (Label)e.Row.FindControl("lblname"); 
      

  5.   


    Label lbl_bh = e.Row.FindControl("lblbh") as Label; 
    Label lbl_name = e.Row.FindControl("lblname") as Label;