[英]DataRow casting its elements to string?
I am trying to get my objects into a DataTable to use it as ItemsSource of a DataGrid. 我正在尝试将我的对象放入DataTable中,以将其用作DataGrid的ItemsSource。
foreach (Translate t in item.Translations)
{
if (rowArray[t.IdSprache] == null)
{
rowArray[t.IdSprache] = t; //here I add the Translate objects to the object[]
}
}
dt.Rows.Add(rowArray); //adding the object[] to the DataTable
var test = dt.Rows[cnt].ItemArray[1] as Translate; //aaaaaaaaaaaaaaand the object is gone (test is always null)
cnt++;
}
Everything works fine, but if i want to cast the elements of the DataRow back to Translate-Objects, i always get null
. 一切正常,但是如果我想将DataRow的元素转换回Translate-Objects,我总是得到
null
。
If I look into the Row's ItemArray
I only see the objects as string ( Assembly.Type
) not as Translate
objects. 如果查看Row的
ItemArray
我只会看到对象是字符串( Assembly.Type
),而不是Translate
对象。
Does anyone know a workaround/solution for this? 有谁知道解决方法/解决方案?
Try using the built in functions on the DataSet
to create the schema and rows. 尝试使用
DataSet
上的内置函数创建架构和行。 Something like: 就像是:
Translate t1 = new Translate() { IdSprache = 0 };
Translate t2 = new Translate() { IdSprache = 3 };
Translate t3 = new Translate() { IdSprache = 7 };
DataTable dt = new DataTable();
for (int i = 0; i < 10; i++)
dt.Columns.Add(new DataColumn("C" + i, typeof(Translate)));
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr[i] = new Translate() { IdSprache = i };
dt.Rows.Add(dr);
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (dt.Rows[i][j] as Translate != null)
Console.Write("T");
else
Console.Write("N");
}
Console.WriteLine();
}
Prints 版画
TNNNN 神经网络
NTNNN 神经网络
NNTNN 神经网络
NNNTN 神经网络
NNNNT 神经网络
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.