获取indexOutOfRange异常

时间:2014-03-20 09:49:17

标签: c# exception datagridview

收到一条例外消息“位置1处没有行”。

如果我们在datatgridview中输入2行或更多行,并且clik the save按钮,它只插入第一行,并给出异常。

这是片段:

private void btnSave_Click(object sender, EventArgs e)
{    
   for (int i = 0; i < dataGridView1.Rows.Count; i++)
   {
         table = obj.select("select id from stock where item_name='" + dataGridView1.Rows[i].Cells[0].FormattedValue + "'");
          obj.query("insert into poi_items (po_id, stock_id, description, expected_price, discount, quantity, expected_total, status, received)values(" + textBox3.Text + "," + table.Rows[i][0] + ",'" + dataGridView1.Rows[i].Cells[1].FormattedValue + "'," + dataGridView1.Rows[i].Cells[3].FormattedValue + "," + dataGridView1.Rows[i].Cells[5].FormattedValue + "," + dataGridView1.Rows[i].Cells[2].FormattedValue + "," + dataGridView1.Rows[i].Cells[6].FormattedValue + ",'Open','0')");                    
   }

 }

1 个答案:

答案 0 :(得分:1)

似乎在您的查询中

table.Rows[i]

是抛出异常的原因。确保表格中有2行。