Search code examples
c#desktop-application

How to get row data from DataGridview after user deleting it


I'm working in a project for a shop and using DataGridView to store all customer orders and update the total price continuously. I update the price when he order more items easily. When I remove a specific item, I need to know its data to update the total price too.

So I need that row's content to make the change I need.

That's what I use to update the total price when he adds more items:

DataGridViewRow row = new DataGridViewRow();

row.CreateCells(pill);

row.Cells[2].Value = name;
row.Cells[1].Value = price;
row.Cells[0].Value = quantity;

pill.Rows.Add(row);

totalPrice += price * quantity;
total.Text = totalPrice.ToString();

Solution

  • There is an event called UserDeletingRow. It accepts DataGridViewRowCancelEventArgs which contains row that user deletes.

    What you need to do is to subscribe to this event, and update your price from there.