从Int.TryParse将object []转换为int [英] Convert object[] to int from a Int.TryParse

查看:92
本文介绍了从Int.TryParse将object []转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数字从一个字符串值的数据表(dt)转换为我想要成为int的另一数据表(dt1).但是我遇到了一个错误:
"无法将类型int隐式转换为对象[]"

I am trying to convert the numbers from one datatable (dt) which are string values, to another datatable (dt1) that I want to be int. But I am getting an error:
''Cannot implicity convert type int to object[]''

DataTable dt1 = new DataTable();
DataRow row;
int obj1;
for ( int x= 0; x<dt.Rows.Count; x++)
{	int.TryParse(dt.Rows[x][0].ToString(), out obj1);
                            
	row = dt1.NewRow();
	row.ItemArray = obj1;
	dt1.Rows.Add(row);
}


特定于obj1.我试图使用row.ItemArray =(int)obj1;但不起作用...


Specific at obj1. I tried to use row.ItemArray = (int)obj1; but does not works...

推荐答案

使用row.ItemArray = (object)obj1;


row.ItemArray是一个对象[]我认为,您不能为其分配一个int.我对您的代码有些困惑.

从表中获取行
row.ItemArray is an object[] I think, you can''t assign an int to it. I''m a bit confused by your code.

to get rows from a table
dt.Rows

其中dt是数据表

访问dr为DataRow的行中的字段

where dt is a DataTable

to access fields in a row where dr is a DataRow

int val = (int)dr["myIntField"];




or

int val = (int)dr[0]; // the first field



这可能会派上用场
http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx



This might come in handy
http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx


这篇关于从Int.TryParse将object []转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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