Answered If item already Exists in DataGridView Update Quantity

csharpnoob

Member
Joined
Dec 22, 2022
Messages
11
Programming Experience
Beginner
hi i want to update qty when add item in DataGridView
If the row with the same data already exists in DataGridView , just ++ the value of Qty . i don't want add new column. every time add item
Capture.JPG

i just want if the row with same data just update qty


here is my code im just learning sorry for bad code :
C#:
            DataTable tblProduct = new DataTable();
            tblProduct.Clear();
            tblProduct = db.readData("select * from Products where Pro_id="+cbxProduct.SelectedValue+"","");
            if (tblProduct.Rows.Count >= 1)
            {
                try
                {
                    string Pro_id = tblProduct.Rows[0][0].ToString();
                    string Pro_Name = tblProduct.Rows[0][1].ToString();
                    string Product_Qty = "1";
                    string Product_buyprice = tblProduct.Rows[0][3].ToString();
                    decimal Product_discount = 0;
                    decimal total = Convert.ToDecimal(Product_Qty) * Convert.ToDecimal(tblProduct.Rows[0][3]);

                    dgvbuy.Rows.Add(1);
                    int rowindex = dgvbuy.Rows.Count - 1;

                    dgvbuy.Rows[rowindex].Cells[0].Value = Pro_id;
                    dgvbuy.Rows[rowindex].Cells[1].Value = Pro_Name;
                    dgvbuy.Rows[rowindex].Cells[2].Value = Product_Qty;
                    dgvbuy.Rows[rowindex].Cells[3].Value = Product_buyprice;
                    dgvbuy.Rows[rowindex].Cells[4].Value = Product_discount;
                    dgvbuy.Rows[rowindex].Cells[5].Value = total;

                }
                catch (Exception)
                {

                }
                try
                {
                    decimal total=0;
                    for (int i =0; i <= dgvbuy.Rows.Count -1;i++)
                    {
                        total += Convert.ToDecimal(dgvbuy.Rows[i].Cells[5].Value);
                        dgvbuy.ClearSelection();
                        dgvbuy.FirstDisplayedScrollingRowIndex = dgvbuy.Rows.Count - 1;
                        dgvbuy.Rows[dgvbuy.Rows.Count - 1].Selected = true;
                    }
                    totalAmount.Text = Math.Round(total , 2).ToString();
                    lblItems.Text = (dgvbuy.Rows.Count).ToString();

                } catch (Exception) { }
            }
        }
 
Instead of just blindly adding another row in your lines 8-23, you need to scan down your rows to find if you already have an existing value.

Your work would be so much easier if you used a data bound data grid view instead of trying to use your view as your data model.
 
thanks for tips will try it

here code not good but do the work if someone also need help

C#:
            bool Found = false;
            if (tblProduct.Rows.Count >= 1)
            {
                try {
                    string Pro_id = tblProduct.Rows[0][0].ToString();
                    string Pro_Name = tblProduct.Rows[0][1].ToString();
                    string Product_Qty = "1";
                    string Product_buyprice = tblProduct.Rows[0][3].ToString();
                    decimal Product_discount = 0;
                    decimal total = Convert.ToDecimal(Product_Qty) * Convert.ToDecimal(tblProduct.Rows[0][3]);

                    foreach (DataGridViewRow row in dgvbuy.Rows)
                    {
                        if (Convert.ToString(row.Cells[1].Value) == cbxProduct.Text && Convert.ToString(row.Cells[3].Value) == Product_buyprice)
                        {
                            row.Cells[2].Value = Convert.ToString(1 + Convert.ToInt16(row.Cells[2].Value));
                            row.Cells[5].Value = Convert.ToDouble(row.Cells[2].Value) * Convert.ToDouble(row.Cells[3].Value);
                            Found = true;
                            return;
                        }
                    }
                    if (!Found)
                    {
                        dgvbuy.Rows.Add(Pro_id, Pro_Name, Product_Qty, Product_buyprice, Product_discount, total);
                    }
                    else
                    {
                        dgvbuy.Rows.Add(Pro_id, Pro_Name, Product_Qty, Product_buyprice, Product_discount, total);
                    }
                }
                catch (Exception) { }
 
Back
Top Bottom