Click here to Skip to main content
15,886,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam retrieving data from Sql server to Datagriedview by scanning barcode,i want when data already exist in dataGriedview only to add quantity.I have tried the code below but when i scan the quantity increase but same row is added down,meaning there are two rows of same barcode which is wrong!Please help

What I have tried:

scannedBarcode = textBoxScanBarcode.Text;
            string query = "SELECT Pid,Pname,ScQty As Qty,Price,Discount As Disc_Limit,ActualDiscount, Price As Sub_Total,Percentage,Barcode FROM products,CostAndPrice WHERE products.Pid=CostAndPrice.Fpid AND Barcode='" + scannedBarcode + "'";
            dbObject.readDatathroughAdapter(query, dtScannedProduct);

            //Boolean to check if he has row has been
            bool Found = false;
            if (dataGridViewPurchasedItems.Rows.Count > 0)
            {
               

                //Check if the product Id exists with the same Price
                foreach (DataGridViewRow row in dataGridViewPurchasedItems.Rows)
                {
                    if (Convert.ToString(row.Cells[9].Value) == scannedBarcode)
                    {
                        //Update the Quantity of the found row
                        row.Cells[3].Value = Convert.ToString(1 + Convert.ToInt16(row.Cells[3].Value));
                        Found = true;
                    }

                }
                if (!Found)
                {
                  
                    //Add the row to grid view
                    dataGridViewPurchasedItems.DataSource = dtScannedProduct;
                }

            }
            else
            {
              
                //Add the row to grid view for the first time
                dataGridViewPurchasedItems.DataSource = dtScannedProduct;
            }
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900