System.ArgumentOutOfRangeException”类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 索引超出范围。必

qq_32320747 2018-02-14 08:35:35

private void btndelete_Click(object sender, EventArgs e)
{
List<DataContract.ClinicInCharge.Records_Moneys> entitys = new List<DataContract.ClinicInCharge.Records_Moneys>();
if (this.dgvlist.RowCount <= 0)
{
}
else
{

entity = entitys[this.dgvlist.CurrentCell.RowIndex];//System.ArgumentOutOfRangeException”类型的未经处理的异常在 mscorlib.dll 中发生 其他信息: 索引超出范围。必须为非负值并小于集合大小
if (entity.id > 0)
{
dal.UpdateData(entity);
}
}
btnQuery_Click(sender, e);

}

我之前点的是第一行也出现这样的问题,求原因
...全文
2168 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
专业的开发人员就好像是谨慎判断大胆治疗的外科大夫一样,而业余的开发人员就好像是“能掐会算、擅长忽悠平民百姓的巫师”一样的风格。那么到底哪一个才更真实?更应该学习呢? 你只要自己动手随便“点点点”鼠标就能发现 entitys 集合的 count 为 0 的情况,缺少的就是动手能力。
  • 打赏
  • 举报
回复
引用 8 楼 duanzi_peng 的回复:
先会调试代码,定位到错误行,再看逻辑。
是的。在自己的开发环境去重现问题,然后使用 vs 调试器调试,这才是专业开发人员。 如果只是给代码“相面”那就不是专业开发人员,只会抄别人的代码。
  • 打赏
  • 举报
回复
entitys 是一个长度为 0 的集合(没有内容的”空“集合)。
zyy1111111 2018-02-26
  • 打赏
  • 举报
回复
数组索引越界,
exception92 2018-02-16
  • 打赏
  • 举报
回复
先会调试代码,定位到错误行,再看逻辑。
hhhljlishen 2018-02-14
  • 打赏
  • 举报
回复
rowindex从1开始的把,改成rowindex-1看行不
秋的红果实 2018-02-14
  • 打赏
  • 举报
回复
首先建议将DataContract.ClinicInCharge.Records_Moneys优化下,如果这条代码有很多运算,加上这么长类型,容易晕 可以开头using DataContract.ClinicInCharge;然后List<Records_Moneys> entitys = new List<Records_Moneys>(); 问题很明显,数组,例如你写entitys[-2],或者写entitys[20],而实际只有19个元素,这些都是错误的写法,只能自己去排查了
道玄希言 2018-02-14
  • 打赏
  • 举报
回复
引用 2 楼 qq_32320747 的回复:
我照你的改,还是这样子。。蟹蟹,我想知道主要是为什么会这样
跟踪调试一下, 看你的 entitys 里面有多少条记录, 然后看下 this.dgvlist.CurrentCell.RowIndex 的值是个什么东东, 应该就可以找到问题了.
土豆赛叩 2018-02-14
  • 打赏
  • 举报
回复
数组索引越界了呗! 看看entitys长度 和 rowindex的值
qq_32320747 2018-02-14
  • 打赏
  • 举报
回复
引用 2 楼 qq_32320747 的回复:
[quote=引用 1 楼 yangb0803 的回复:] if (this.dgvlist == null || this.dgvlist.RowCount <= 0) { } else { if (entitys != null && this.dgvlist.CurrentCell.RowIndex <= entitys.Count) { entity = entitys[this.dgvlist.CurrentCell.RowIndex]; if (entity.id > 0) { dal.UpdateData(entity); } } }
我照你的改,还是这样子。。蟹蟹,我想知道主要是为什么会这样[/quote] 如果我点了行数的话,发现entitys.count=0;要怎么解决
qq_32320747 2018-02-14
  • 打赏
  • 举报
回复
引用 1 楼 yangb0803 的回复:
if (this.dgvlist == null || this.dgvlist.RowCount <= 0) { } else { if (entitys != null && this.dgvlist.CurrentCell.RowIndex <= entitys.Count) { entity = entitys[this.dgvlist.CurrentCell.RowIndex]; if (entity.id > 0) { dal.UpdateData(entity); } } }
我照你的改,还是这样子。。蟹蟹,我想知道主要是为什么会这样
道玄希言 2018-02-14
  • 打赏
  • 举报
回复
if (this.dgvlist == null || this.dgvlist.RowCount <= 0) { } else { if (entitys != null && this.dgvlist.CurrentCell.RowIndex <= entitys.Count) { entity = entitys[this.dgvlist.CurrentCell.RowIndex]; if (entity.id > 0) { dal.UpdateData(entity); } } }

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧