用户删除该行后,如何从C#中的DataGridview中的特定行保存数据

问题描述

我正在为商店设计项目,并使用DataGridView存储所有客户订单并不断更新总价。当他轻松订购更多物品时,我会更新价格。删除特定商品时,我也需要知道其数据以更新总价。

所以我需要该行的内容来进行所需的更改。

当他添加更多商品时,这就是我用来更新总价的方式:

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();

解决方法

有一个名为UserDeletingRow的事件。它接受DataGridViewRowCancelEventArgs,其中包含用户删除的行。

您需要做的就是订阅此活动,并从那里更新价格。