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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace form1
{
public partial class Form1 : Form
{
List<Student> data = GetStudents();
public Form1()
{
InitializeComponent(); this.dataGridView1.DataSource = data;
} public static List<Student> GetStudents()
{ return new List<Student>()
{
new Student{ ID =,Name="小a",Age=},
new Student{ ID =,Name="小b",Age=},
new Student{ ID =,Name="小c",Age=},
new Student{ ID =,Name="小d",Age=},
new Student{ ID =,Name="小e",Age=},
new Student{ ID =,Name="小f",Age=},
new Student{ ID =,Name="小g",Age=},
new Student{ ID =,Name="小k",Age=} }; }
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)
{
data.Remove(student);
// students.Add(student);
}
} for (int i = ; i < students.Count(); i++)
{
data.Remove(students[i]);
}
this.dataGridView1.DataSource = null;
this.dataGridView1.DataSource = data;
}
}
}
问题说明:右键删除行的时候异常,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行的值就超出了索引。
winform删除dataGridView列报异常:System.IndexOutOfRangeException:“索引 7 没有值的更多相关文章
- MVC4删除 pages引发的异常 System.Web.Optimization找不到引用
在MVC4的开发中,如果创建的项目为空MVC项目,那么在App_Start目录下没有BundleConfig.cs项的内容,在手动添加时在整个库中都找不到:System.Web.Optimizatio ...
- SQLServer\framework启动报异常:Module的类型初始值设定项引发异常
net framework卸载 重装 https://download.microsoft.com/download/E/4/1/E4173890-A24A-4936-9FC9-AF930FE3FA4 ...
- 集合遍历过程iterator, 添加删除元素报异常
list set 遍历过程中添加或者删除元素,报异常. 使用iterator 也会报异常 ConcurrentModificationException remove只能用迭代器的remove,而 ...
- DataGridView编辑实时生效和索引-1没有值问题
1. 问题:DataGridView单元格编辑后,只有离开焦点时,编辑的内容才会生效(在绑定的DataSource中生效). 使用 this.dataGridView1.CommitEdit(Dat ...
- C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常
在DataGridView手动添加了CheckBox列;在窗体Show的时候,遇到一个错误:错误如下: DataGridView中发生一下异常:System.FormatException:单元格的F ...
- [Winform]DataGridView列自适应宽度
引言 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的. 方法 ...
- DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。
其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...
- DataGridView中的ComboboxCell报了System.ArgumentException:DagaGridViewComboBoxCell值无效错误
原因是初始化的时候给ComboboxCell绑定了一系列的值,但是真正赋值的时候却给了一个不在那一系列值范围中的值,所以就报了这个错 在开发的时候难免会因为数据的问题出现这个问题,为了不让系统崩掉,就 ...
- 异常:System.Data.EvaluateException: 未找到列[District].
异常:System.Data.EvaluateException: 未找到列[District]. 这里存在的问题不一定是说,数据源表没有该字段.此问题在于数据库字段包含空格字符.
随机推荐
- Spring IOC容器装配Bean_基于注解配置方式
bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...
- python原类、类的创建过程与方法
今天为大家介绍一下python中与class 相关的知识-- 获取对象的类名 python是一门面向对象的语言,对于一切接对象的python来说,咱们有必要深入的学习与了解一些知识 首先大家都知道,要 ...
- 1.flask基础
1.flask和django的区别? flask,是一个轻量级的框架,内置了:路由/视图/模板(jinja2)/cookie/session/中间件. 可扩展强,第三方组件非常多,例如:wtforms ...
- TortoiseGit 设置ssh方式
TortoiseGit使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥. 也就是说使用 ssh-keygen -t rsa -C "576953565@qq.com& ...
- Java修炼——四种方式解析XML_DOM
四种方式解析XML:DOM JDOM DOM4J SAX 先写一个XML栗子: <?xml version="1.0" encoding="UT ...
- POJ 1966 Cable TV NETWORK(网络流-最小点割集)
Cable TV NETWORK The interconnection of the relays in a cable TV net ...
- python 2.7编码问题
问题引入 先看下面的代码,代码用utf8编码格式保存. print("中") 仅有一行代码,但是这个代码无论在ubuntu下还是win7下都会报错,错误信息类似是下面的内容: Sy ...
- NETCore Bootstrap Admin 通用后台管理权限 [3]: 精简版任务调度模块
前言 NETCore 里说到任务调度,大家首先想到的应该是大名鼎鼎的 QuartzNET 与 Hangfire,然而本篇介绍的却都不是,而是 Bootstrap Admin(以下简称 BA)通用后台权 ...
- 冒泡排序 C&&C++
冒泡排序(因为过程像冒泡,所以叫做冒泡排序) 流程: (1)对数组中各个数字,一次比较相邻两个 (2)如果前面大于后面,就交换这两个数据 (3)再用同样的方法继续排,直到外层循环排完 或者 (1) ...
- 【玩转SpringBoot】给自动配置来个整体大揭秘
上一篇文章中提到的条件注解,只是自动配置整体解决方案中的一个环节而已,可以说是管中窥豹. 本文就逐步擦除迷雾,让整体浮现出来,这样就会有一个宏观的认识. 除了写代码之外,还能干点什么? 提到“配置”这 ...