简体   繁体   中英

Datagridview cell value not updated when modified dynamically

Based on the design requirements, the datagridview can not be edited directly by the user. 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. However, the raw keyboard input needs to be formatted before it goes in the cell. So, I intercept the KeyPress events as follows:

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. 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

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. 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)

Try DataGridView.EndEdit method.

Commits and ends the edit operation on the current cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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