欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

winform删除dataGridView列报异常:System.IndexOutOfRangeException:“索引 7 没有值

程序员文章站 2022-05-31 22:03:05
winform界面如下: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using S ......

winform界面如下:

winform删除dataGridView列报异常:System.IndexOutOfRangeException:“索引 7 没有值

 1 using system;
 2 using system.collections.generic;
 3 using system.componentmodel;
 4 using system.data;
 5 using system.drawing;
 6 using system.linq;
 7 using system.text;
 8 using system.threading.tasks;
 9 using system.windows.forms;
10 
11 namespace form1
12 {
13     public partial class form1 : form
14     {
15         list<student> data = getstudents();
16         public form1()
17         {
18             initializecomponent();
19 
20             this.datagridview1.datasource = data;
21         }
22 
23 
24         public static list<student> getstudents()
25         {
26 
27             return new list<student>()
28             {
29                 new student{ id =1,name="小a",age=18},
30                 new student{ id =2,name="小b",age=18},
31                 new student{ id =3,name="小c",age=18},
32                 new student{ id =4,name="小d",age=18},
33                 new student{ id =5,name="小e",age=18},
34                 new student{ id =6,name="小f",age=18},
35                 new student{ id =7,name="小g",age=18},
36                 new student{ id =8,name="小k",age=18}
37 
38             };
39 
40         }
41         private void tsmdelete_click(object sender, eventargs e)
42         {
43 
44             list<student> students = new list<student>();
45             foreach (datagridviewrow row in this.datagridview1.selectedrows)
46             {
47                 var student = row.databounditem as student;
48                 if (student != null)
49                 {
50                      data.remove(student);
51                    // students.add(student);
52                 }
53             }
54            
55             for (int i = 0; i < students.count(); i++)
56             {
57                 data.remove(students[i]);
58             }
59             this.datagridview1.datasource = null;
60             this.datagridview1.datasource = data;
61         }
62     }
63 }

问题说明:右键删除行的时候异常,system.indexoutofrangeexception:“索引 7 没有值。

winform删除dataGridView列报异常:System.IndexOutOfRangeException:“索引 7 没有值

 

 

删除的代码如下:

 private void tsmdelete_click(object sender, eventargs e)
        {
          
            foreach (datagridviewrow row in this.datagridview1.selectedrows)
            {
                var student = row.databounditem as student;
                if (student != null)
                {
                     data.remove(student);    
                }
            }
            this.datagridview1.datasource = null;
            this.datagridview1.datasource = data;
        }

修改后的代码:

        private void tsmdelete_click(object sender, eventargs e)
        {

            list<student> students = new list<student>();
            foreach (datagridviewrow row in this.datagridview1.selectedrows)
            {
                var student = row.databounditem as student;
                if (student != null)
                {                   
                students.add(student);
                }
            }

            for (int i = 0; i < students.count(); i++)
            {
                data.remove(students[i]);
            }
            this.datagridview1.datasource = null;
            this.datagridview1.datasource = data;
        }

异常的原因分析:

 this.datagridview1.selectedrows获取选中的行,假设删除的是第7行和第8行。

遍历去取删除这两行,类型datagridviewrow 直接引用数据源中的值。第7行删除以后总行数就变成了7行 row.databounditem去根据索引取第8行的值就超出了索引。