Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 993
Thracian Avatar asked May 11 '11 04:05

Thracian


1 Answers

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();
like image 140
JohnZ Avatar answered Sep 25 '22 23:09

JohnZ