简体   繁体   English

动态修改时不更新 Datagridview 单元格值

[英]Datagridview cell value not updated when modified dynamically

Based on the design requirements, the datagridview can not be edited directly by the user.根据设计要求,datagridview 不能由用户直接编辑。 It is in read-only mode.它处于只读模式。 When the user double-clicks on the cell, the datagridview's read-only property becomes false and the cell accepts keyboard input.当用户双击单元格时,datagridview 的只读属性变为 false 并且单元格接受键盘输入。 However, the raw keyboard input needs to be formatted before it goes in the cell.但是,原始键盘输入需要在进入单元格之前进行格式化。 So, I intercept the KeyPress events as follows:所以,我拦截 KeyPress 事件如下:

private void dgw_keyPress(object sender, KeyPressEventArgs e)
 {
     e.Handled = true;
 }

At this point the cell is in edited mode and dirty mode.此时单元格处于编辑模式和脏模式。 Then I update the Value property in a different method and call dgw.Refresh() which is supposed to display the updated value on the cell.然后我用不同的方法更新 Value 属性并调用dgw.Refresh() ,它应该在单元格上显示更新的值。 But it won't.但不会。 it will update only when the current cell is not dirty and is not in edit mode.仅当当前单元格不脏且未处于编辑模式时才会更新。 How can I force the cell display the updated value while it is still in edit mode?如何在单元格仍处于编辑模式时强制显示更新的值?

Any ideas?有任何想法吗?

Use below to refresh the current cell's value, change to suit your EditingControl type使用下面刷新当前单元格的值,更改以适合您的 EditingControl 类型

if (dgvMain.EditingControl is TextBox)
{
    dgvMain.EditingControl.Text = dgvMain.CurrentCell.Value.ToString();
}

Another method:另一种方法:

Call this method to force a cell to update its display value in edit mode.调用此方法可强制单元格在编辑模式下更新其显示值。 This is useful when an external process modifies the cell value and you want to notify the user of the change, even when a user-specified change is lost as a result.当外部进程修改单元格值并且您想要通知用户更改时,即使用户指定的更改因此丢失,这也很有用。 details 细节

dgvMain.RefreshEdit();

我用下面的代码解决了。

GrdBudgetTabOver.EndEdit()

This will work,这将工作,

dgv.EndEdit()
dgv.InvalidateCell(ColIdx, RowIdx)

You might be able to do that by implementing the IDataGridViewEditingControl interface.您可以通过实现IDataGridViewEditingControl接口来做到这一点 I think that's the way to get the most control over how the cell enters and leaves edit mode.我认为这是最大程度地控制单元格进入和离开编辑模式的方式。 You can find more details in section 5.11 of Mark Rideout's DataGridView FAQ (DOC)您可以在 Mark Rideout 的DataGridView FAQ (DOC) 的第 5.11 节中找到更多详细信息

Try DataGridView.EndEdit method.尝试DataGridView.EndEdit方法。

Commits and ends the edit operation on the current cell.提交并结束对当前单元格的编辑操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM