10
votes

So I've been running into some trouble tonight with my c# windows forms application. Is it possible to insert a new row to a datagridview when the currently selected new row is still on its default values? Or, if I want to change the values programmatically, how can I emulate the user editing the textbox in order to finalize the row?

To clarify, when a row is a new row (and shows up as true with the isNewRow property), I cannot figure out how to change out of the isNewRow = true state to generate a new empty row below.

I've tried dataGridView.EndEdit(), dataGridView.CurrentRow.DataGridView.EndEdit(), bindingSource.EndEdit(), and none of these have really done the trick. (also, using dataGridView.BeginEdit() puts the text box into editmode, which isn't handy, because I can't seem to edit the cell programmatically after that)

Ideally, I would like to be able to continue to hit enter on new rows, while changing their values with a custom control, or not, and have a new row appear underneath.

Does anyone have any solutions to this?

1
better show the code you've tried .. - matzone
Is this data grid view bound to any background data object? - Denise Skidmore
Have you tried Validate? msdn.microsoft.com/en-us/library/… "To commit row changes programmatically, call the form's Validate method. If your data source is a BindingSource, you can also call BindingSource.EndEdit." msdn.microsoft.com/en-us/library/… - Denise Skidmore
Yes, I am using a binding source. Using the validate method only seems to clear the cell, and BindingSource.EndEdit does not do anything - Harrison Lambeth
I'd also like to add that the issue is only when adding a new row. With the same code, I can currently make changes to old rows just fine. The problem seems to be that I can't commit new rows to the binding source. - Harrison Lambeth

1 Answers

11
votes

Okay so I figured how to make this work. When modifying the values using my custom control, I had to use

bindingSource.EndEdit();
dataGridView.NotifyCurrentCellDirty(true);
dataGridView.EndEdit();
dataGridView.NotifyCurrentCellDirty(false);

So whenever changes are made to the new row, it forces the row to be committed and adds a new blank row space to the end of the datagridview.

I did not have to use Validate(), however.