索引超出异常范围 [英] Index was outside the bound of an exception

查看:72
本文介绍了索引超出异常范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在从网格更新应用程序中的数据表.我正在使用此代码更新数据表,但由于索引在数组外部,因此它引发了错误.谁能帮我解决这个问题?这是代码:

Hi,

I am updating the datatable in my application from the grid. I am using this code to update the datatable but it throws an error as index was outside the array. Can any one help me to solve this problem Here is the code:

DataRow dr = dtScope.Select("PackageCode='" + grdBoq.Rows[i].Cells[0].Text + "' and SubPackageCode='" + grdBoq.Rows[i].Cells[0].Text + "'")[0];
               dr["SolutionEngineerCode"] = ddlSolutionEngineer.SelectedValue;
               dr["SloutionEngineer"] = ddlSolutionEngineer.SelectedItem;


谢谢&问候


Thanks & Regards

推荐答案

那么i的值是多少?出现这种错误的原因是由于您使用的索引.

So what is the value of i? The reason for this kind of error is due to the index you are using.

grdBoq.Rows[i].Cells[0].Text



检查i的值是否大于网格视图的行数.或您的gridview为空.如果是这样,请尝试添加以下代码:



Check if the value of i is bigger than the number of rows of your grid view. Or your gridview is empty. If it is, try adding this code:

if(grdBoq.Rows.Count != 0)
{
DataRow dr = dtScope.Select("PackageCode='" + grdBoq.Rows[i].Cells[0].Text + "' and SubPackageCode='" + grdBoq.Rows[i].Cells[0].Text + "'")[0];
               dr["SolutionEngineerCode"] = ddlSolutionEngineer.SelectedValue;
               dr["SloutionEngineer"] = ddlSolutionEngineer.SelectedItem;
}



如果这解决了您的问题,请标记为正确答案

最好的问候,
爱德华(Eduard)



If this solved your problem, please mark as correct answer

Best regards,
Eduard


问题是我们没有错误的上下文,所以我们不能告诉你:就是这样-做到这一点".
您将不得不自己更详细地研究问题.

我将如何处理:
将它们分成单独的行,并将它们分配给临时局部变量.所以:
The problem is that we have no context fro the error, so we can''t tell you: "it''s this - do that".
You will have to look at the problem in more detail yourself.

How I would handle it:
Break each of those into separate lines, and assign them to temporary local variables. So:
DataRow dr = dtScope.Select("PackageCode=''" + grdBoq.Rows[i].Cells[0].Text + "'' and SubPackageCode=''" + grdBoq.Rows[i].Cells[0].Text + "''")[0];

将成为:

grdBoqRowI = grdBoq.Rows[i];
grdBoqCell = grdBoqRowI.Cells[0];
DataRow dr = dtScope.Select("PackageCode=''" + grdBoqCell.Text + "'' and SubPackageCode=''" + grdBoqCell.Text + "''")[0];

,依此类推.然后,使用调试器在导致错误的行上获取异常,然后可以检查相关数据以确定原因.

And so on. Then, using the debugger you will get the exception on the line that causes the error, and you can examine the relevant data to determine why.


这篇关于索引超出异常范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆