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
i just want if the row with same data just update qty
here is my code im just learning sorry for bad code :
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
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) { }
}
}