DataGridView throwing “InvalidOperationException: Operation is not valid…” when adding a row

前端 未结 5 1619
失恋的感觉
失恋的感觉 2021-02-07 05:00

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,

5条回答
  •  迷失自我
    2021-02-07 05:51

    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.

提交回复
热议问题