简体   繁体   English

C#System.ArgumentOutOfRangeException:索引超出范围

[英]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.

相关问题 System.ArgumentOutOfRangeException:索引超出范围 - System.ArgumentOutOfRangeException: Index was out of range c#System.ArgumentOutOfRangeException - c# System.ArgumentOutOfRangeException System.ArgumentOutOfRangeException C# - System.ArgumentOutOfRangeException C# C#和SQL System.ArgumentOutOfRangeException - C# and SQL System.ArgumentOutOfRangeException C#应用程序中的System.ArgumentOutOfRangeException - System.ArgumentOutOfRangeException in C# Application System.ArgumentOutOfRangeException LINQ C# - System.ArgumentOutOfRangeException linq c# System.ArgumentOutOfRangeException发生在C#中 - System.ArgumentOutOfRangeException occurred in C# System.ArgumentOutOfRangeException: '指定的参数超出了有效值的范围。 参数名称:asp.net中的index' - System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net 错误:System.ArgumentOutOfRangeException:索引超出范围。 必须为非负数且小于集合的大小 - Error: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection System.ArgumentOutOfRangeException: '索引超出范围。 从一个列表复制到另一个 - System.ArgumentOutOfRangeException: 'Index was out of range. copy from one list to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM