我有一个DataGridView其中有3列;数量,费率和金额。
DataGridView是可编辑的。当我在汇率栏中输入一个值时,立即应该在金额中更改该值。单元格值更改事件,c#
Amount=Qty*rate
它发生,但是当我点击任何其他细胞,我想,当我在速率输入任何值应该与数量相乘,并在数额上立即反映在不改变小区。
我有一个DataGridView其中有3列;数量,费率和金额。
DataGridView是可编辑的。当我在汇率栏中输入一个值时,立即应该在金额中更改该值。单元格值更改事件,c#
Amount=Qty*rate
它发生,但是当我点击任何其他细胞,我想,当我在速率输入任何值应该与数量相乘,并在数额上立即反映在不改变小区。
如果您确实想在不更改单元格的情况下更新该值(如在飞行中),则必须处理DataGridView.KeyPress事件并检查哪个单元正在更新。
如果这太麻烦了,请使用DataGridView.CellValueChanged事件。实现比KeyPress事件更简单。
正如Sachin Shanbhag提到的那样,您应该同时使用DataGridView.CurrentCellDirtyStateChanged
和DataGridView.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;
}
}
使用CommitEdit,CurrentCellDirtyStateChanged的确按预期工作,每次在脏状态发生变化时(即用户更改字符串)都会触发它,谢谢Dmitry! – 2013-12-23 09:24:46
我发现没有事件可以正确处理单元格更改的值。
您必须将可编辑单元格转换为文本框,然后在其上提供更改的事件。
这是我发现在浏览MSDN论坛的一个代码:
我也是在这里添加代码:
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs
{
MessageBox.Show("changed");
}
你尝试过哪些活动?尝试DataGridView.CurrentCellDirtyStateChanged事件 – Thakur 2010-09-29 10:34:11