我有这个代码从数据网格中检索数据然后存储在另一个数据表中。但我得到的例外是:
"Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.Data.DataRowView'.
“此异常发生在"dt.Rows.Add(dr.ItemArray)"
如果有任何缺陷或需要任何其他信息,请告诉我。
代码:
DataTable dt = new DataTable();
DataColumn c1 = new DataColumn("IsActive", typeof(bool));
dt.Columns.Add(c1);
DataColumn c2 = new DataColumn("DataGridTextBox_QCList1", typeof(string));
c2.MaxLength = 500;
dt.Columns.Add(c2);
DataColumn c3 = new DataColumn("DataGridTextBox_QCSummary", typeof(string));
c3.MaxLength = 500;
dt.Columns.Add(c3);
DataColumn c4 = new DataColumn("DataGridComboxBox_Control", typeof(string));
c4.MaxLength = 500;
dt.Columns.Add(c4);
foreach (DataRowView d in dtgQCNumbers.Items)
{
DataRow dr = d.Row;
dt.Rows.Add(dr.ItemArray);
dt.AcceptChanges();
}
foreach (DataRow row in dt.Rows)
{
qcId = row["QC_ID"].ToString();
zipFolderPath = baseFolderPath;
patternFiles = Directory.GetFiles(zipFolderPath, "*.zip");
logMessage = "Unzipping file from path" + zipFolderPath + " \n file name:" + patternFiles[0];
Common.LogMessage(logMessage);
UnZipReleasePatch(zipFolderPath, patternFiles.First());
//deploy release patch.
logMessage = "Deploying files" + zipFolderPath;
Common.LogMessage(logMessage);
DeployFiles();
}
答案 0 :(得分:2)
DataGrid.Items
可以包含NewItemPlaceholder
,用于允许用户添加新项目。出于这个原因,您不能无差别地迭代集合,要么过滤掉该项目,要么迭代ItemsSource
。
答案 1 :(得分:0)
感谢AVD,得到的答案如下:
foreach (DataRow d in _ds.Tables[0].Rows)
{
if (d["DataGridComboxBox_Control"].ToString() == "AVS_DB")
{
qcId = d["DataGridTextBox_QCList1"].ToString();
zipFolderPath = baseFolderPath;
patternFiles = Directory.GetFiles(zipFolderPath, "*.zip");
logMessage = "Unzipping file from path" + zipFolderPath + " \n file name:" + patternFiles[0];
Common.LogMessage(logMessage);
UnZipReleasePatch(zipFolderPath, patternFiles.First());
logMessage = "Deploying files" + zipFolderPath;
Common.LogMessage(logMessage);
DeployFiles();
}
}