7/10/2014

Increment the value of Qty if row with the same data already exists in DataGridView

Increment the value of Qty if row with the same data already exists in DataGridView
Increment the value of Qty if row with the same data already exists in DataGridView


private void btn_Add_Click(object sender, EventArgs e)  
     {  
       //Boolean value to check if the DataGridView has the same value.  
       bool Found = false;  
       double total = Convert.ToDouble(txt_Price.Text) * 1;  
       if (dataGridView1.Rows.Count > 0)  
       {  
         //Check if the product Name exists with the same Price  
         foreach (DataGridViewRow row in dataGridView1.Rows)  
         {  
           if (Convert.ToString(row.Cells[0].Value) == txt_Name.Text && Convert.ToString(row.Cells[2].Value) == txt_Price.Text)  
           {  
             //Update the Quantity of the found row  
             row.Cells[1].Value = Convert.ToString(1 + Convert.ToInt16(row.Cells[1].Value));  
             row.Cells[3].Value = Convert.ToDouble(row.Cells[1].Value) * Convert.ToDouble(row.Cells[2].Value);  
             Found = true;  
           }  
         }  
         if (!Found)  
         {  
           //Add the row to DataGridView if data not present  
           dataGridView1.Rows.Add( txt_Name.Text,1, txt_Price.Text,total);  
         }  
       }  
       else  
       {  
         //Add the first row to DataGridView if there is no row  
         dataGridView1.Rows.Add( txt_Name.Text, 1, txt_Price.Text,total);  
       }  
     }  

4 comments:

  1. nice
    but i need it visual basic how it is pz

    ReplyDelete
  2. why iam retrieving the data by using barcode and put those data in dataGriedview but it add another row and qty increase:sample code below



    DataTable dtScannedProduct=new DataTable();
    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).Equals(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;
    }

    ReplyDelete
  3. What if to reverse the add by deleting the rows using save idea? How to do that by clicking on a del button in the same DatagridView Button Cell?

    ReplyDelete