7

I've an editable unbounded datagridview. I'm changing the value of new row programmatically.

Normally, when user types in any field of a new row, it becomes dirty and another new row is inserted below it.

But in my case, when user comes in to any field of a new row, I'm trapping the function key and changing the cell value programmatically.

myGrid.CurrentCell.Value = "xyz";

And it doesn't insert a new row below it.

Now as a work around I tried this on CellValueChanged event handler.

  if (myGrid.NewRowIndex == e.RowIndex)
  {
    myGrid.Rows.Insert(e.RowIndex + 1, 1);
  }

But it throws error saying No row can be inserted after the uncommitted new row..

How could I tell myGrid that I've made current row (which is a new row) dirty and that there is need of a new row after it?

IsmailS
  • 10,797
  • 21
  • 82
  • 134

4 Answers4

9

I am working on winforms DataGridView. In my case i tried @iSid solution, but here is an aid in that, i tried this

myGrid.NotifyCurrentCellDirty(true);
myGrid.NotifyCurrentCellDirty(false);

By only making current cell dirty in not working, you have to commit that cell also. In the next command i am flagging the current cell is not dirty, this will enforce datagridview to add a dirty row. It is working in my project.

Haider Ali Wajihi
  • 2,756
  • 7
  • 51
  • 82
8

Here I got the solution

myGrid.NotifyCurrentCellDirty(true);
IsmailS
  • 10,797
  • 21
  • 82
  • 134
  • I voted for this, but with more testing I found that I needed to ALSO NotifyCurrentCellDirty(False) as suggested by @Haider – isedwards Jan 19 '17 at 07:58
0

This is really really late considering the question timeframe, but the accepted answer did not work for me. What ended up resolving it for me was first inserting a row above the dirty row, then editing that 'clean' row instead.

if (e.RowIndex == myGrid.Rows.Count - 1)
{
    myGrid.Rows.Add();
}
myGrid[e.ColumnIndex, e.RowIndex].Value = etd.ResultText; // Edit at your leisure.
Renan Gemignani
  • 2,683
  • 1
  • 21
  • 23
  • Did you focused on the concerned cell before calling `myGrid.NotifyCurrentCellDirty(true);`? – IsmailS Sep 10 '13 at 06:13
  • Yes, the user had to double-click on the cell before the system would fill it. It worked well the first time, but behavior on repeated inserting wouldn't work. – Renan Gemignani Sep 10 '13 at 12:09
  • I'm in web development right now. So cannot verify your solution. :( Will wait for somebody to upvote before I can tick your answer. – IsmailS Sep 11 '13 at 10:20
0

I think (I'm not sure, though), that you're trying to insert a row after the New Row...which is not possible. Changing your code to this

myGrid.Rows.Insert(e.RowIndex - 1, 1);

Should insert the row before the New Row, which should work.

Bobby
  • 11,419
  • 5
  • 44
  • 69
  • I don't need row above current row. I'm changing the new row itself and on its change I need another new row because the current row is dirty after change. I've got the solution. I'm posting it shortly. – IsmailS Dec 21 '10 at 11:03