0

我有这段代码可以从数据网格中检索数据,然后将其存储在另一个数据表中。但我得到的例外是: "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();
}
4

3 回答 3

2

DataGrid.Items可以包含一个NewItemPlaceholder用于允许用户添加新项目的内容。出于这个原因,您不能不加选择地迭代集合,要么过滤掉该项目,要么迭代它ItemsSource

于 2012-08-29T09:44:11.903 回答
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();
             }
         }
于 2012-08-28T02:19:59.197 回答
0

如果您不需要添加新行,您可以简单地在 XAML 中编写:

CanUserAddRows="假"

并且占位符将丢失,因此您可以迭代直到再次打开它。

于 2022-02-07T14:56:56.587 回答