Thracian
Thracian

Reputation: 661

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?

Upvotes: 5

Views: 21296

Answers (5)

GerryC
GerryC

Reputation: 21

This will work,

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

Upvotes: 0

user4618214
user4618214

Reputation: 21

I solved with the code below.

GrdBudgetTabOver.EndEdit()

Upvotes: 0

JohnZ
JohnZ

Reputation: 1427

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();

Upvotes: 12

Jacob Seleznev
Jacob Seleznev

Reputation: 8151

Try DataGridView.EndEdit method.

Commits and ends the edit operation on the current cell.

Upvotes: 0

Don Kirkby
Don Kirkby

Reputation: 56230

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)

Upvotes: 0

Related Questions