BlueRaja - Danny Pflughoeft BlueRaja - Danny Pflughoeft - 2 months ago 21
C# Question

DataGridView throwing "InvalidOperationException: Operation is not valid..." when adding a row

I want an OpenFileDialog to come up when a user clicks on a cell, then display the result in the cell.

It all works, except that the DataGridView displays an extra row, for adding values to the list it's bound to. The row shows up if

dataGridView.AllowUserToAddNewRows == true
, which is what I want. What I don't want is for the application to crash when that row is edited programatically; instead, it should do exactly what it would do if the user had edited that row manually (add the new row to the underlying list, push another empty row onto the grid for adding values).

I read about SendKeys.Send(), which should make the DataGridView behave exactly as though the user had typed the value in; however, it does not work either. Here is what I am trying:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.CurrentCell = cell;

//simply doing a cell.Value = etc. will cause the program to crash
cell.ReadOnly = false;
dataGridView1.Columns[cell.ColumnIndex].ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.BeginEdit(true);
SendKeys.Send(openFileDialog1.FileName + "{Enter}");
dataGridView1.EndEdit();
cell.ReadOnly = true;
dataGridView1.Columns[cell.ColumnIndex].ReadOnly = true;
}
//I would expect the FileName would be in the cell now, and a new empty
//row tacked onto the end of the DataGridView, but it's not; the DataGridView
//is not changed at all.

Answer

I was having the same problem when trying to programattically edit cells with a binding source. ""Operation is not valid due to the current state of the object"

Which operation? What State? So helpful.

My code seem to work fine except when editing the last row in the grid.

Turns out the key is DataGridView.NotifiyCurrentCelldirty(true)

The correct sequence for programatically editing a cell, so it works the same as if the user did it. (A new empty row appears when changing a cell in the last row) is something like this:

1) Make the cell to edit the current cell (do what ever you need to the current currentcell, first like calling endEdit if it is in edit mode.)

2) Call DataGridview.BeginEdit(false)

3) Call DataGridView.NotifyCurrentCellDirty(true)

4) Modify the value.

5) Call DataGridView.EndEdit()

And you'll want to do something for the RowValidating and RowValidated events.

One of my routines for updating a cell value looks like this:

This is from a method in my class derived from DataGridView. You could do the same thing from the containing form, calling through a DataGridView instance, because the methods are public. Here the calls are using an impliciit 'this.'

    private void EnterTime()
    {
        if (CurrentRow == null) return;

        SaveCurrentCell(); // Calls EndEdit() if CurrentCell.IsInEditMode
        DataGridViewCell previous = CurrentCell;

        CurrentCell = CurrentRow.Cells[CatchForm.TimeColumn];
        BeginEdit(false);
        NotifyCurrentCellDirty(true);
        CurrentCell.Value = DateTime.Now;
        EndEdit();

        CurrentCell = previous;

    }

I’m not sure why a separate call is needed.

Why doesn’t BeginEdit, or actually modifying the cell value, cause the right things to happen?

And if you move the NotifyCurrentCellDirty call to after you actually modify the cell, it doesn’t behave correctly either. All very annoying.