2012-02-11 73 views
0

我正在将XML文件读入数据集,然后添加一个新列,然后为该列中的每一行分配一个值。未分配数据表项目数组

我的程序创建新列,我可以看到它具有BitmapImage数据类型,通过在调试屏幕中查看它,同时它的运行和使用messagebox来toString其数据类型。

但是,当我尝试将某些东西分配给该行的列时,它仍然是'{}',当我尝试使用它时,它会给出错误'Unable to cast type DBNull to BitmapImage';

ItemDS = new DataSet(); 
ItemDS.ReadXml(homeFolder + @"Items.xml", XmlReadMode.InferSchema); 
ItemDS.Tables[0].Columns.Add("pic", typeof(BitmapImage)); 
MessageBox.Show(ItemDS.Tables[0].Columns[5].DataType.ToString()); 
foreach (DataRow theRow in ItemDS.Tables[0].Rows) 
{ 
    try 
    { 
     theRow.ItemArray[5] = (SquareImageFromFile(NewDeployFolder + @"assets\images\items\" + theRow.ItemArray[3].ToString(), 120)); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
    combItem.Items.Add(theRow.ItemArray[0]); 
} 

当我运行赋值行时,没有错误出现,但在运行该行之后没有发生任何事情。

回答

1

我没有用太多DataTable,但是我希望ItemArray做出复制数据的,而不是返回一个“活”的阵列,其中DataRow轨道。我真的没有料到会工作。尽量只通过索引设置值:

theRow[5] = (SquareImageFromFile(NewDeployFolder + @"assets\images\items\" + 
           theRow[3].ToString(), 120)); 

ItemArray真的意味着获得或散装设定值,而不是单个值。

+0

呃,我就是这样一个小丑,谢谢你修复我的语法,现在这个作品完美无缺! – user1178412 2012-02-11 09:38:27