Cell Value Changing Event ,c#

前端 未结 3 1849
陌清茗
陌清茗 2021-01-21 22:48

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

3条回答
  •  灰色年华
    2021-01-21 23:19

    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;
        }
    }
    

提交回复
热议问题