DataTable dt2 = ht["多收费记录"] as DataTable;
 for (int i = 0; i < dt2.Rows.Count; i++)
 {
      DataGridViewRow row = new DataGridViewRow();
      this.DataGridView1.Rows.Add(row);
      row.Cells["yhdm"].Value = dt2.Rows[i]["bank_id"].ToString();
      row.Cells["lsbh"].Value = dt2.Rows[i]["id"].ToString();
      row.Cells["rq"].Value = dt2.Rows[i]["tpdate"].ToString();
      row.Cells["ss"].Value = dt2.Rows[i]["realpay"].ToString();
      row.Cells["khh"].Value = dt2.Rows[i]["custid"].ToString();
      row.Cells["khdbh"].Value = dt2.Rows[i]["client"].ToString();
      row.Cells["qqm"].Value = dt2.Rows[i]["reqcode"].ToString();
}
我的代码是这样的,其中dt2中有200多行数据,在for循环加载时候,i=0的时候,可以通过,当i=1的时候,就报错了
提示指定的参数已超出有效值的范围。参数名: rowIndex
row.Cells["yhdm"].Value 引发了System.ArgumentOutOfRangeException异常
 

解决方案 »

  1.   

    换一种添加方式.
    this.dgv.Rows.Add(); //添加空行
    //设置新添空行值
     this.dgv.Rows[i].Cells[1].Value="";
      

  2.   


    DataGridViewRow row = new DataGridViewRow();
         
          row.Cells["yhdm"].Value = dt2.Rows[i]["bank_id"].ToString();
          row.Cells["lsbh"].Value = dt2.Rows[i]["id"].ToString();
          row.Cells["rq"].Value = dt2.Rows[i]["tpdate"].ToString();
          row.Cells["ss"].Value = dt2.Rows[i]["realpay"].ToString();
          row.Cells["khh"].Value = dt2.Rows[i]["custid"].ToString();
          row.Cells["khdbh"].Value = dt2.Rows[i]["client"].ToString();
          row.Cells["qqm"].Value = dt2.Rows[i]["reqcode"].ToString();
     this.DataGridView1.Rows.Add(row);
    换个顺序试试
      

  3.   

    好象是你的dt2超出范围了,你试一下这个哦.DataTable dt2 = ht["多收费记录"] as DataTable;
     for (int i = 1; i < dt2.Rows.Count; i++)
     {
          DataGridViewRow row = new DataGridViewRow();
          this.DataGridView1.Rows.Add(row);
          row.Cells["yhdm"].Value = dt2.Rows[i-1]["bank_id"].ToString();
          row.Cells["lsbh"].Value = dt2.Rows[i-1]["id"].ToString();
          row.Cells["rq"].Value = dt2.Rows[i-1]["tpdate"].ToString();
          row.Cells["ss"].Value = dt2.Rows[i-1]["realpay"].ToString();
          row.Cells["khh"].Value = dt2.Rows[i-1]["custid"].ToString();
          row.Cells["khdbh"].Value = dt2.Rows[i-1]["client"].ToString();
          row.Cells["qqm"].Value = dt2.Rows[i-1]["reqcode"].ToString();
    }