Click here to Skip to main content
12,695,404 members (26,043 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0
I received the error:"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;
            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)
                DataTable dscrntProd = prods.SelCrntProd4Sale(crntProdID);//get data from sql server database for product details(productID, ProductName,ProductPrice)
                foreach (DataRowView dr in dscrntProd.Rows)

Any help Most Appreciated
Posted 3-Jan-13 6:17am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

foreach (DataRowView dr in dscrntProd.Rows)

You cannot cast a DataRow into DataRowView.
Try foreach (DataRow dr in dscrntProd.Rows) instead.
Marcus Kramer 3-Jan-13 13:13pm
Abhinav S 3-Jan-13 22:00pm
Thank you Marcus.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100