winform刪除dataGridView列報異常:System.IndexOutOfRangeException:「索引 7 沒有值

winform界面以下:ide

 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 }
View Code

問題說明:右鍵刪除行的時候異常,System.IndexOutOfRangeException:「索引 7 沒有值。this

 

 

刪除的代碼以下:spa

 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;
        }

修改後的代碼:code

        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;
        }

異常的緣由分析:orm

 this.dataGridView1.SelectedRows獲取選中的行,假設刪除的是第7行和第8行。blog

遍歷去取刪除這兩行,類型DataGridViewRow 直接引用數據源中的值。第7行刪除之後總行數就變成了7行 row.DataBoundItem去根據索引取第8行的值就超出了索引。索引

相關文章
相關標籤/搜索