用户代码未处理 System.ArgumentOutOfRangeException

sunxh_2004 2015-09-22 04:57:18
用户代码未处理 System.ArgumentOutOfRangeException
Message=指定的参数已超出有效值的范围。
参数名: rowIndex
Source=System.Windows.Forms
ParamName=rowIndex
StackTrace:
在 System.Windows.Forms.DataGridViewRow.GetState(Int32 rowIndex)
在 System.Windows.Forms.DataGridViewRow.get_Displayed()
在 System.Windows.Forms.DataGridView.OnClearedRows()
在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
在 System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)
在 System.Windows.Forms.DataGridView.OnClearingColumns()
在 System.Windows.Forms.DataGridViewColumnCollection.Clear()
在 System.Windows.Forms.DataGridView.Dispose(Boolean disposing)
在 System.ComponentModel.Component.Dispose()
在 System.Windows.Forms.Control.Dispose(Boolean disposing)
在 System.ComponentModel.Component.Dispose()
在 System.Windows.Forms.Control.Dispose(Boolean disposing)
在 System.ComponentModel.Component.Dispose()
在 System.Windows.Forms.Control.Dispose(Boolean disposing)
在 System.Windows.Forms.ContainerControl.Dispose(Boolean disposing)
在 System.ComponentModel.Component.Dispose()
在 System.Windows.Forms.Control.Dispose(Boolean disposing)
在 System.Windows.Forms.Form.Dispose(Boolean disposing)
在 ZZ.App.Client.UI.MonitorMainForm.Dispose(Boolean disposing) 位置 C:\Documents and Settings\bxzz\桌面\客户端\ZzMonitorSolution\ZZ.App.Client.MonitorDemo\Main\MonitorMainForm.Designer.cs:行号 20
在 System.ComponentModel.Component.Dispose()
在 System.Windows.Forms.ApplicationContext.Dispose(Boolean disposing)
在 System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows()
InnerException:


C#中 的问题,请教 怎么解决呀!
...全文
459 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ajianchina 2015-09-23
  • 打赏
  • 举报
回复
说点实在话吧 既然你都已知错误类型了,怎么不这样去捕捉呢? catch(System.ArgumentOutOfRangeException ex) 你就可以看到问题所在了
sunxh_2004 2015-09-23
  • 打赏
  • 举报
回复
我知道是DataGridView 有问题,错误提示是:
system.IndexOutOfRangeException: 索引 0 没有值。
但是具体出错位置,没有捕获到。
private void dgvCoilInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if (dgvCoilInfo.Columns[e.ColumnIndex].Name == "C_Lenght" && e.RowIndex > -1 && e.Value != null && e.Value.ToString().Trim().Length > 0)
{
e.Value = double.Parse(e.Value.ToString()) / 1000;
}
}
catch (Exception ex)
{
WriteLog(ex, Tag.ToString());
}
}

private void dgvCoilInfoCategory_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if (dgvCoilInfoCategory.Columns[e.ColumnIndex].Index == 8 && e.RowIndex > -1 && e.Value != null && e.Value.ToString().Trim().Length > 0) {
e.Value = double.Parse(e.Value.ToString()) / 1000;
}
}
catch (Exception ex)
{
WriteLog(ex, Tag.ToString());
}
}

private void dgvDefectDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if (dgvDefectDetail.Columns[e.ColumnIndex].Index == 1 && e.RowIndex > -1 && e.Value != null && e.Value.ToString().Trim().Length > 0)
{
switch (e.RowIndex)
{
case 0:
case 1:
case 2:
case 3:
break;
case 4:
Code defectType = codeUtil.GetCode(CodeType.DefectCategory.ToString(), e.Value.ToString());
if (defectType != null)
{
e.Value = defectType.Name;
}
break;
case 5:
string value = e.Value.ToString();
string[] val = value.Split('/');
if (val.Length > 1)
{
Code productionType = codeUtil.GetCode(CodeType.DefectCategory.ToString(), val[0]);
Code testType = codeUtil.GetCode(CodeType.DefectCategory.ToString(), val[1]);
if (productionType != null)
{
value = productionType.Name;
}
value += "/";
if (testType != null)
{
value += testType.Name;
}
}
else
{
Code productionType = codeUtil.GetCode(CodeType.DefectCategory.ToString(), e.Value.ToString());
if (productionType != null)
{
value = productionType.Name;
}
}
e.Value = value;
break;
case 6:
Code credible = codeUtil.GetCode(CodeType.Credible.ToString(), e.Value.ToString());
if (credible != null)
{
e.Value = credible.Name;
}
break;
case 7:

string name = zzMDLLevel1.GetLevelName(dgvDefectDetail[1, 4].Value.ToString());
if (name != null)
{
e.Value = name;// level.Name;
}
break;
case 8:
e.Value += " 米";
break;
case 9:
e.Value += " 毫米";
break;
case 10:
e.Value += " 毫米";
break;
case 11:
e.Value += " 毫米";
break;
case 12:
e.Value += " 毫米";
break;
case 13:
e.Value += " 平方毫米";
break;
case 14:
case 15:
break;
case 16:
Code vidicon = codeUtil.GetCode(CodeType.Vidicon.ToString(), e.Value.ToString());
if (vidicon != null)
{
e.Value = vidicon.Name;
}
break;
default:
break;
}
}
}
catch (Exception ex)
{
WriteLog(ex, Tag.ToString());
}
}
这三个代码有什么问题吗?请教
ajianchina 2015-09-23
  • 打赏
  • 举报
回复
你平时从不注意观察对方的情绪变化 你没有考虑对方是否有条件接受你的需求 你也不会征询对方是否愿意 你没有考虑过对此产生的可能性后果 你更没有想过产生后果所需付出的责任 你强行就上了
xian_wwq 2015-09-23
  • 打赏
  • 举报
回复
引用 楼主 sunxh_2004 的回复:
用户代码未处理 System.ArgumentOutOfRangeException Message=指定的参数已超出有效值的范围。 参数名: rowIndex Source=System.Windows.Forms ParamName=rowIndex StackTrace: 在 System.Windows.Forms.DataGridViewRow.GetState(Int32 rowIndex) 在 System.Windows.Forms.DataGridViewRow.get_Displayed() 在 System.Windows.Forms.DataGridView.OnClearedRows() 在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell) 在 System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell) 在 System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow) 在 System.Windows.Forms.DataGridView.OnClearingColumns() 在 System.Windows.Forms.DataGridViewColumnCollection.Clear() 在 System.Windows.Forms.DataGridView.Dispose(Boolean disposing) 在 System.ComponentModel.Component.Dispose() 在 System.Windows.Forms.Control.Dispose(Boolean disposing) 在 System.ComponentModel.Component.Dispose() 在 System.Windows.Forms.Control.Dispose(Boolean disposing) 在 System.ComponentModel.Component.Dispose() 在 System.Windows.Forms.Control.Dispose(Boolean disposing) 在 System.Windows.Forms.ContainerControl.Dispose(Boolean disposing) 在 System.ComponentModel.Component.Dispose() 在 System.Windows.Forms.Control.Dispose(Boolean disposing) 在 System.Windows.Forms.Form.Dispose(Boolean disposing) 在 ZZ.App.Client.UI.MonitorMainForm.Dispose(Boolean disposing) 位置 C:\Documents and Settings\bxzz\桌面\客户端\ZzMonitorSolution\ZZ.App.Client.MonitorDemo\Main\MonitorMainForm.Designer.cs:行号 20 在 System.ComponentModel.Component.Dispose() 在 System.Windows.Forms.ApplicationContext.Dispose(Boolean disposing) 在 System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows() InnerException: C#中 的问题,请教 怎么解决呀!
重点查对DataGridView有删除操作的代码 在使用Index的时候,先判定所涉及的元素是不是存在 -------------------- 比如在遍历的过程中删除成员,则必然会导致ArgumentOutOfRangeException
sunxh_2004 2015-09-23
  • 打赏
  • 举报
回复
程序是C/S模式的,程序运行一段时间就会报上述错误,有时错误报如下代码处: 21 static void Main(string[] args) 22 { 23 Application.EnableVisualStyles(); 24 Application.SetCompatibleTextRenderingDefault(false); 25 try 26 { 27 SpringContextHolder context = SpringContextHolder.getInstance(); 28 context.Init(); 29 } 30 catch (Exception e) 31 { 32 MessageUtils.ShowMsg("连接数据库服务器失败" + e.ToString(), ExceptionLevel.ERROR); 33 return; 34 } 35 CodeUtil.GetInstance().InitCode(); //初始化代码 36 SetModenByArgs(args); //设置登录模式 37 //Thread.CurrentThread.IsBackground = true; 38 try 39 { 40 MonitorMainForm mainForm = new MonitorMainForm(); 41 Application.Run(mainForm); 42 } 43 catch (Exception ex) 44 { 45 MessageBox.Show("异常:" + ex.ToString()); 46 return; 47 } 48 } 这代码是客户端的。
sunxh_2004 2015-09-23
  • 打赏
  • 举报
回复
感谢各位的回复,MonitorMainForm.Designer.cs:行号 20 代码如下:

14 protected oveerride void Dispose(bool disposing)
15 {
16 if (disposing && (components != null ))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
错误定位到这里,我不知道为什么啦,请教各位。
  • 打赏
  • 举报
回复
你对gridview做了啥事情,居然在designer里面报异常!
为轮子而生 2015-09-23
  • 打赏
  • 举报
回复
意思是你这个rowindex超出了范围,实际上如果你要引用某一行的话,rowindex的有效范围应该是0~(RowCount-1),一旦发生引用.Rows[rowindex]而rowindex又超出这个范围的话,就会出现你这个异常。 原因有很多,可能你这个rowindex在使用时,指向的行已不存在,或者在Datagridview的事件中,e.RowIndex为-1(也就是由ColumnHeader触发的事件),具体原因你还是要追踪到MonitorMainForm.Designer.cs:行号 20这里来看。 下次发帖时,最好不要发这么多堆栈调用上来,堆栈调用是给你自己看的,而“Designer.cs:行号 20”附近的代码才是给我们看的
sunxh_2004 2015-09-23
  • 打赏
  • 举报
回复
catch(System.IndexOutOfRangeException ex) 也没能扑捉到,请教各位哦。程序运行后,间隔不同时间就上述错误,我也是刚学c# 有些知识还不懂。
sunxh_2004 2015-09-23
  • 打赏
  • 举报
回复
这个 catch(System.ArgumentOutOfRangeException ex) 我添加上去了,没捕获到,感觉也很奇怪,我的开发环境是在winxp 下,vS2010环境中运行.net4.0下。
EdsionWang 2015-09-22
  • 打赏
  • 举报
回复
贴这么多没用的,啥也没描述出来。 在 ZZ.App.Client.UI.MonitorMainForm.Dispose(Boolean disposing) 位置 C:\Documents and Settings\bxzz\桌面\客户端\ZzMonitorSolution\ZZ.App.Client.MonitorDemo\Main\MonitorMainForm.Designer.cs:行号 20 去这里找找问题吧

110,539

社区成员

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

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

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