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]. 这里存在的问题不一定是说,数据源表没有该字段.此问题在于数据库字段包含空格字符.
随机推荐
- 最小生成树与最短路径--C语言实现
接昨天,在这里给出图的其中一种应用:最小生成树算法(Prime算法和Kruskal算法).两种算法的区别就是:Prime算法以顶点为主线,适合用于顶点少,边密集的图结构:Kruskal算法以边为主线, ...
- 2019-2020-1 20199304《Linux内核原理与分析》第六周作业
第五章 系统调用的三层机制(下) 1.往MenuOS中添加命令 (1)首先进入LinuxKernel文件夹,将menu目录删除.然后再git clone克隆下载更新了版本之后的menu目录(包含tim ...
- gulp+webpack+angular1的一点小经验(第二部分webpack包起来的angular1)
又一周过去了,项目也已经做得有点模样了.收集来一些小经验,分享给大家,有疏漏之处,还望指正,海涵. 上周整合了gulp与webpack,那么工具准备差不多了,我们就开始编码吧.编码的框架就是angul ...
- 上手spring boot项目(二)之spring boot整合shiro安全框架
题记:在学习了springboot和thymeleaf之后,想完成一个项目练练手,于是使用springboot+mybatis和thymeleaf完成一个博客系统,在完成的过程中出现的一些问题,将这些 ...
- nmon脚本——对Linux服务器的监控
继服务器被挖之后,我又开拓了另一个监控工具----nmon! Nmon可以很轻松的监控系统的CPU.内存.网络.硬盘.文件系统.NFS.高耗进程.资源和IBM Power系统的微分区的信息,还有专属的 ...
- flex布局的兼容问题
一.W3C各个版本的flex 2009 version 标志:display: box; or a property that is box-{*} (eg. box-pack) 2011 versi ...
- openstack网络(一)
什么是OpenStack Networking OpenStack网络的特性 交换机 路由器 负载均衡 防火墙 虚拟专用网络 网络功能虚拟化 网络类型 管理网络 API 网络 外部网络 访客网络 在一 ...
- 3- 设置断点修改Response
以下是借鉴别人的知识分享.我在这里转载,如有冒犯,还请告知. 只要你会设置断点修改请求内容的话,这个设置断点修改响应内容的方法也是一样的,只需要修改一下命令即可. 修改响应内容也有两种方法: 第一种: ...
- 【JS】302- 回调地狱解决方案之Promise
为什么出现Promise 在javascript开发过程中,代码是单线程执行的,同步操作,彼此之间不会等待,这可以说是它的优势,但是也有它的弊端,如一些网络操作,浏览器事件,文件等操作等,都必须异步执 ...
- Matlab Robotics Toolbox 仿真计算:Kinematics, Dynamics, Trajectory Generation
1. 理论知识 理论知识请参考: 机器人学导论++(原书第3版)_(美)HLHN+J.CRAIG著++贠超等译 机器人学课程讲义(丁烨) 机器人学课程讲义(赵言正) 2. Matlab Robotic ...