错误:indexoutofrange异常 [英] Error : indexoutofrange Exception

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

问题描述

仅在代码下方出现错误

i get error below code only

str2 += strListItems[i] + ":" + ds.Tables[0].Rows[comboBox1.SelectedIndex][i].ToString() + "\r\n";



错误是indexoutofrange Exception.
我的完整代码是



the error is indexoutofrange Exception.
and my full code is

string str2 = "";
            string[] strListItems = { ds.Tables[0].Columns[1].ToString(), ds.Tables[0].Columns[0].ToString(), ds.Tables[0].Columns[2].ToString() };
            for (int i = 0; i < ds.Tables[0].Rows.Count -1; i++)
            {
                str2 += strListItems[i] + ":" + ds.Tables[0].Rows[comboBox1.SelectedIndex][i].ToString() + "\r\n";
            }
            textBox1.Text = str2;


请给我任何解决的办法

谢谢
VenkatS


Please give me any idea to resolve it

Thanks
VenkatS

推荐答案

因此,如果ds.Tables [0]中有多于3行,它们应该去哪里?

您需要查看strListItems的定义-用固定的行数声明它,但不要检查是否在该行之外.
So, if there are more than 3 rows in ds.Tables[0] where are they supposed to go?

You need to look at your definition of strListItems - you declare it with a fixed number of rows, but do not check if you are outside that.


问题是如果ds.Tables[0].Rows.Count具有大于3,则会在strListItems中引发错误,该错误具有3个值.因为您将strListItems放入for循环中,所以请考虑一下表是否包含更多行,这就是引发错误的原因.
The problem is If the ds.Tables[0].Rows.Count has more than 3 then it will raise error in strListItems which has 3 values. Because you put the strListItems in for loop, just think if the table contains more rows, that''s why it raised the error.


这篇关于错误:indexoutofrange异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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