使用数据表在第2位没有行 [英] There is no row at position 2 using datatable

查看:80
本文介绍了使用数据表在第2位没有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙伴!!!

我正在使用它。

Hi mates !!!
I am using this.

DataTable objDt = new DataTable();
string a = objDt.Rows[0][0].ToString();
string b = objDt.Rows[1][0].ToString();
string c = objDt.Rows[2][0].ToString();





查询可能返回取决于,表示可能是1,2或3直到3行。



如果查询返回2行, string c = objDt.Rows [2] [0] .ToString(); 此声明有错误。



请帮助



Query may return depends, that means may be 1,2 or 3. until 3 rows.

If query returns 2 rows, string c = objDt.Rows[2][0].ToString(); this statement has error.

Please Help

推荐答案

做一行在访问行之前计算检查。所以基本上你在访问它之前检查是否存在一行。

Do a row count check before you access a row. So basically you check if a row exist or not before accessing it.
string a , b, c;
if (objDt.Rows.Count > 0)
    a = objDt.Rows[0][0].ToString();
if (objDt.Rows.Count > 1)
    b = objDt.Rows[1][0].ToString();
if (objDt.Rows.Count > 2)
    c = objDt.Rows[2][0].ToString();






ofcourse,datatable会抛出错误,如果你试试如果行数为2,则访问第三行。



如果行计数为2,则只能访问行索引0,1。但是你正在尝试访问索引2,它超过了no。的行。



所以,字符串c = objDt.Rows [2] [0] .ToString()抛出错误如果行数是2.





快乐编码。: - )
Hi,

ofcourse, datatable will throw error, if you try to access third row if the row count is 2.

if Row count is 2, then row index 0,1 can only be accessible. but you are trying access index 2, which exceeds the no. of rows.

so, string c = objDt.Rows[2][0].ToString() throw error if the row count is 2.


happy coding.:-)


你可能有used Convert.Tostring()



它允许空值并且不会抛出异常
you could have used Convert.Tostring()

It allows Null values and does not throw exception


这篇关于使用数据表在第2位没有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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