Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取IndexOutfrange异常_C#_Exception_Datagridview - Fatal编程技术网

C# 获取IndexOutfrange异常

C# 获取IndexOutfrange异常,c#,exception,datagridview,C#,Exception,Datagridview,获取异常消息“位置1没有行” 如果我们在datatgridview中输入2行或更多行,并点击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

获取异常消息“位置1没有行”

如果我们在datatgridview中输入2行或更多行,并点击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')");                    
   }

 }
private void btnSave\u单击(对象发送者,事件参数e)
{    
对于(int i=0;i
在您的查询中

table.Rows[i]

是引发异常的原因。请确保表中有2行。

如果调试并在for循环中放置断点,dataGridView1.rows是什么样子的?当它指向datagridview的第二行并执行第二个查询时,它会在dataGridView1.rows[i].Cells[6]处向我显示异常.FormattedValueRows在调试器中的某一点上是否有两个条目?只是出于兴趣,计数是否会确保在这种情况下有两行?或者如果在行[1]仍然为空时更新计数,这里是否存在线程问题?@Richard Count放在gridviewrows上,而不是放在表行上。