I have a DataGridView in which there are 3 columns; Quantity, Rate and Amount.
The DataGridView is Editable. When I enter a value in the Rate Column then immediately the val
As it was mentioned by Sachin Shanbhag you should use both DataGridView.CurrentCellDirtyStateChanged
and DataGridView.CellValueChanged
events. In DataGridView.CurrentCellDirtyStateChanged
you should check whether the user is modifying the right cell (Rate in your case) and then execute DataGridView.CommitEdit method. Here is some code.
private void YourDGV_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (YourDGV.CurrentCell.ColumnIndex == rateColumnIndex)
{
YourDGV.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void YourDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == rateColumnIndex)
{
DataGridViewTextBoxCell cellAmount = YourDGV.Rows[e.RowIndex].Cells[amountColumnIndex];
DataGridViewTextBoxCell cellQty = YourDGV.Rows[e.RowIndex].Cells[qtyColumnIndex];
DataGridViewTextBoxCell cellRate = YourDGV.Rows[e.RowIndex].Cells[rateColumnIndex];
cellAmount.Value = (int)cellQty.Value * (int)cellRate.Value;
}
}