views:

293

answers:

3

I'm using the following code to update a DataGridView's cell value. This is called inside the 'CellDoubleClick' event handler for the DataGridView.

The problem is the cell only updates after I click onto another cell. Is there a better way of doing this?

string value = "";
if (_dataGrid1.CurrentRow.Cells[e.ColumnIndex].Value != null)
    value = _dataGrid1.CurrentRow.Cells[e.ColumnIndex].Value.ToString();

FormEdit form = new FormEdit(value); // takes a string value
if (form.ShowDialog() == DialogResult.OK)
{
    _dataGrid1.CurrentRow.Cells[e.ColumnIndex].Value = form.SourceText;
    _dataGrid1.UpdateCellValue(e.ColumnIndex, e.RowIndex);
}

Some variable names were changed to protect their identity

A: 

Does calling _dataGrid1.CommitEdit solve the problem?

Eric
A: 

Try calling Refresh on the control. The data may be updated, but the visual representation may not be redrawn until the click elsewhere.

Larry Watanabe
A: 

I returned to the project that this is for today, and discovered my answer was easy. Suspend/ResumeLayout was the solution:

if (form.ShowDialog() == DialogResult.OK)
{
    _dataGridView.SuspendLayout();

    _dataGridView.CurrentRow.Cells[e.ColumnIndex].Value = form.TextBoxText;
    _dataGridView.UpdateCellValue(e.ColumnIndex, e.RowIndex);

    _dataGridView.ResumeLayout(true);
}

This was for double clicking a gridview cell, and editing the content in a modal form with a textbox.

Chris S