我接收到的錯誤:無法轉換類型的對象的System.Data.DataRow'爲類型「System.Data.DataRowView
Unable to cast object of type 'System.Data.DataRow' to type 'System.Data.DataRowView'.
private void addToOrder(DataRowView drv)
{
DataRow dr = orderDataTable.NewRow();
dr["ProductID"] = drv["ProductID"];
dr["ProductBarcode"] = drv["ProductBarcode"];
dr["ProductName"] = drv["ProductName"];
dr["SalePrice"] = drv["SalePrice"];
dr["SoldQuantity"] = 1;
dr["NotEditable"] = false;
try
{
orderDataTable.Rows.Add(dr);
}
catch (ConstraintException)
{
DataRow dr1 = orderDataTable.Rows.Find(drv["ProductID"]);
dr1["SoldQuantity"] = Convert.ToInt32(dr1["SoldQuantity"]) + 1;
}
}
private void btnAddToOrder_Click(object sender, RoutedEventArgs e)
{
// add to order
if(txtBarcodeAuto.Text.Length >0)
{
//get data from sql server database for product details(productID, ProductName,ProductPrice)
DataTable dscrntProd = prods.SelCrntProd4Sale(crntProdID);
foreach (DataRowView dr in dscrntProd.Rows)
{
addToOrder(dr);
}
}
}
不錯,趕上! +1 .. –
除了你的結論之外,他不需要編寫'GetOrderDataRowViewFromDataRow',而是改變'addToOrder'的簽名來使用'DataRows'作爲@Reacher所提到的。 –