【问题标题】:DataGridView geting row index in c# gets exception out of rangeDataGridView在c#中获取行索引导致异常超出范围
【发布时间】:2016-04-13 04:25:09
【问题描述】:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e){

menoDB = dataGridView1.Rows[idSelectedRow].Cells["meno"].Value.ToString();      
priezviskoDB = dataGridView1.Rows[idSelectedRow].Cells["priezvisko"].Value.ToString();       
kontaktDB = dataGridView1.Rows[idSelectedRow].Cells["kontakt"].Value.ToString();      
zaplatenetDB = dataGridView1.Rows[idSelectedRow].Cells["platene"].Value.ToString();
}

idSelectedRow = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["idludia"].Value.ToString());

它在前 2 行中运行良好,但是当我点击第 3 行时,它从第 10 行(最后一行)开始读取,然后当我点击第 4 行时,我得到了这个异常。

System.ArgumentOutOfRangeException 未处理

很明显它尝试从第 11 行读取,但没有第 11 行。

【问题讨论】:

  • 根据你的最后一行,看起来你已经解决了。

标签: c# mysql datagridview outofrangeexception


【解决方案1】:

这一行的问题:

idSelectedRow = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["idludia"].Value.ToString());

现在在您的 idSelectedRow 中,您没有行索引,但有 idludia 值。所以用这种方式改变你的代码:

menoDB = dataGridView1.Rows[e.RowIndex].Cells["meno"].Value.ToString();      
priezviskoDB = dataGridView1.Rows[e.RowIndex].Cells["priezvisko"].Value.ToString();       
kontaktDB = dataGridView1.Rows[e.RowIndex].Cells["kontakt"].Value.ToString();      
zaplatenetDB = dataGridView1.Rows[e.RowIndex].Cells["platene"].Value.ToString();

【讨论】:

  • 非常感谢。它解决了我的问题。现在实际上我什至不需要 idSelectedRow,因为我不会在其他任何地方使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多