简体   繁体   中英

Make new row dirty programmatically and insert a new row after it in DataGridView

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?

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.

我在这里得到了解决方案

myGrid.NotifyCurrentCellDirty(true);

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM