I have a data grid view with QUANTITY, UNIT PRICE and VALUE columns. What I want to do is when changing the QUANTITY I need to change the VALUE . VALUE = QUANTITY * UNIT PRICE
I wrote code for gridview 'CellValueChanged' event. But then VALUE will change when leaving the cell only. I need to change VALUE when typing the QUANTITY.
My code like this.Any idea please.
private void dgvSale_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dgvSale.Columns[e.ColumnIndex].Name == "Qty")
{
double qty = Convert.ToDouble(dgvSale["Qty", dgvSale.CurrentRow.Index].Value);
double rate = Convert.ToDouble(dgvSale["Rate", dgvSale.CurrentRow.Index].Value);
double totVal = qty * rate;
dgvSale["Value", dgvSale.CurrentRow.Index].Value = totVal.ToString("#,###.00");
}
}
I don't know if I understood you right. You're trying to update the value on the fly, while typing? If so then you probably should use also DataGridView.CurrentCellDirtyStateChanged event to commit cell value changes. Below is the example based on MSDN:
private void dgvSale_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvSale.IsCurrentCellDirty)
{
dgvSale.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Is that what you need?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With