选择标头时,datagridview中的索引超出范围异常 [英] Index out of range exception in datagridview when header is selected
本文介绍了选择标头时,datagridview中的索引超出范围异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个datagridview,当我单击标题进行排序或出于任何原因时,在下面的代码行中出现跟随错误...。
I have a datagridview that when I click on a header to sort or for any reason I get the follow error on the following line of code....
争论我们的范围异常(索引超出范围。必须为非负且小于集合的大小。
private void firearmView_CellClick(object sender, DataGridViewCellEventArgs e)
{
//I get the above error on the IF line below.
if (!firearmView.Rows[e.RowIndex].IsNewRow)
{
selectedFirearmPictureBox.Image = Image.FromFile(firearmView.Rows[e.RowIndex].Cells[12].Value.ToString(), true);
}
}
我不知道为什么会收到此特定错误
I do not know why I am getting this particular error here.
推荐答案
MSDN在文档中对 RowIndex属性
The MSDN says in the docs about RowIndex property
当RowIndex属性返回-1时,单元格要么是
When the RowIndex property returns -1, the cell is either a column header, or the cell's row is shared.
所以您需要处理e.RowIndex == -1当您收到事件
(...索引不得为负....)
So you need to handle the e.RowIndex == -1 when you receive the event
(...The index must not be negative....)
private void firearmView_CellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex == -1) return;
if (!firearmView.Rows[e.RowIndex].IsNewRow)
{
selectedFirearmPictureBox.Image = Image.FromFile(firearmView.Rows[e.RowIndex].Cells[12].Value.ToString(), true);
}
}
这篇关于选择标头时,datagridview中的索引超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文