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]. 这里存在的问题不一定是说,数据源表没有该字段.此问题在于数据库字段包含空格字符.
随机推荐
- 微信小程序——详细讲解页面传值(多种方法)
1.使用navigator的url带参传值 (1)在pageA页面有一个固定的值要传递到pageB页面,比如说一个固定的值user_id要传递给B <navigator url=".. ...
- 从“职场小白”进阶为“行业大牛”,四个"锦囊"教你破局
在早期软件行业,会存在一个普遍的现象,有些大学的本科,或者研究生毕业,他们去面试工作的时候会发现,面试下来代码能力可能不是太好,这种情况下公司会问你愿不愿意去做测试? 如果说早期软件测试行业还是一个风 ...
- 洛谷 题解 SP3267 【DQUERY - D-query】
今天机房讲了莫队. 但是蒟蒻我并没有听懂,所以晚上回家恶补,才弄明白莫队. 莫队是莫涛大神发明的,它的作用就是用优秀的复杂度求解于一些区间之间的操作,莫队其实就是一个优雅的暴力,它的复杂度是O(n s ...
- 一篇文章教你轻松使用fastjson
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y JSON相信大家对他也不陌生了,前后端交互中常常 ...
- Open-Pit Mining
Description Open-pit mining is a surface mining technique of extracting rock or minerals from the ea ...
- 原生js 复制内容到剪切板
代码: function copyHandle(content){ let copy = (e)=>{ e.preventDefault() e.clipboardData.setData('t ...
- uni-app微信小程序开发之引入腾讯视频小程序播放插件
登录微信小程序管理后台添加腾讯视频播放插件: 正式开始使用腾讯视频小程序插件之前需先在微信公众平台 -> 第三方设置 -> 插件管理处添加插件,如下图所示: 在uni-app中引入插件代码 ...
- 基于iCamera测试宽动态全局摄像头mt9v034或mt9v032小结
基于iCamera测试宽动态全局摄像头mt9v034或mt9v032小结 先看看硬件特点 主要是:全局快门.宽动态 然后看看硬件测试,usb采集出图 再看看寄存器配置 主要测试,可以修改分辨率,修改曝 ...
- 【系列专题】ECMAScript 重温系列(10篇全)
ES6 系列ECMAScript 2015 [ES]150-重温基础:ES6系列(一) [ES]151-重温基础:ES6系列(二) [ES]152-重温基础:ES6系列(三) [ES]153-重温基础 ...
- CCF-CSP题解 201809-4 再卖菜
碎碎念..近视加老花,还以为第二天除了第二家范围在100以内别的都不确定,于是x**算的记搜复杂度超时了.还鼓捣着什么差分区间最长路,虽然有大神用差分区间做出来了,然而自己并没有看懂. 其实就是一个记 ...