Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView update Cell Value while typing

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");
    }
 }
like image 552
Tom Avatar asked Jun 20 '14 06:06

Tom


1 Answers

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?

like image 97
Patryk Spytkowski Avatar answered Sep 21 '22 10:09

Patryk Spytkowski