细胞价值变化事件,c#

Nag*_*mar 2 c# datagridview winforms

我有一个DataGridView,其中有3列; 数量,费率和金额.
DataGridView是可编辑的.当我在Rate Column中输入一个值时,应立即在Amount中更改该值.

Amount=Qty*rate
Run Code Online (Sandbox Code Playgroud)

它正在发生,但是当我点击任何其他单元格时,我希望当我在Rate中输入任何值时,它应该与Quantity相乘并立即反映在Amount中而不更改单元格.

Dmi*_*hin 5

正如Sachin Shanbhag所提到的,你应该同时使用DataGridView.CurrentCellDirtyStateChangedDataGridView.CellValueChanged事件.在DataGridView.CurrentCellDirtyStateChanged您应检查用户是否修改右边的单元格(在您的情况),然后执行DataGridView.CommitEdit方法.这是一些代码.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)