[英]C# System.ArgumentOutOfRangeException:Index was out of range
I need some help, I'm kinda new to this problem.. 我需要一些帮助,我对此问题有点陌生。
DataGridViewCell cell = null;
foreach (DataGridViewCell selectedCell in dataGridView1.SelectedCells)
{
cell = selectedCell;
break;
}
if (cell != null)
{
DataGridViewRow row = cell.OwningRow;
idgene.Text = row.Cells[0].Value.ToString();
first.Text = row.Cells[1].Value.ToString();
last.Text = row.Cells[2].Value.ToString();
mid.Text = row.Cells[3].Value.ToString();
address.Text = row.Cells[4].Value.ToString();
yeaar = row.Cells[5].Value.ToString();
}
This is the first time I encountered this kind of problem, Every time I add the combobox.text this gives me this error,System.ArgumentOutOfRangeException:Index was out of range,Must be non-negative and less than the size of the collection. 这是我第一次遇到此类问题,每次添加combobox.text时,都会出现此错误,System.ArgumentOutOfRangeException:索引超出范围,必须为非负数且小于集合的大小。
I also tried this this but it didn't solved my problem: 我也尝试了这个,但是并没有解决我的问题:
if (cell != null)
{
string yeaar = "";
syear.Text = yeaar;
DataGridViewRow row = cell.OwningRow;
idgene.Text = row.Cells[0].Value.ToString();
first.Text = row.Cells[1].Value.ToString();
last.Text = row.Cells[2].Value.ToString();
mid.Text = row.Cells[3].Value.ToString();
address.Text = row.Cells[4].Value.ToString();
yeaar = row.Cells[5].Value.ToString();
}
change your if statement to 将您的if语句更改为
(this is using System.Linq) (这是使用System.Linq的)
if (cell != null && cell.OwningRow.Cells.Count() >= 6) 如果(单元格!= null && cell.OwningRow.Cells.Count()> = 6)
and if you don't have enough cells it won't execute so you will have to account for that 如果没有足够的单元,它将无法执行,因此您必须考虑到这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.