DataGridView, BindingNavigator, and New Row

Hi,

I have a DataGridView and a BindingNavigator bound to the same BindingSource (which is in its turn bound to a DataTable from a typed DataSet).
DataGridView.AllowUserToAddRow = True, so the New Row is visible.

I've noticed that clicking the BindingNavigator.AddNewItem button does not select the New Row. In stead, it creates a new record at the end of the DGV, just before the New Row.

Is this default behaviour, or does it behave like this because of something in my code
(Sorry if this is explained somewhere else and I missed it. Seems like a straightforward scenario to me, but I couldn't find any explanation.)

Because of this behaviour, calculating and setting default values for the new row isn't as straightforward as it seems anymore.

If I set DataGridView.AllowUsersToAddRow = False, I can still add new rows with the BindingNavigator.AddNewItem button. But I cannot set default values through the AddingNewEventArgs.NewObject because my data source is a DataTable (explained in Help for the AddingNewEventArgs class).

With DataGridView.AllowUsersToAddRow = True, I can set the new row's cells using the DataGridView.DefaultValuesNeeded event, but it does not get fired when the user clicks the AddNewItem button.

Any suggestions

Tnx,
flip.be



Answer this question

DataGridView, BindingNavigator, and New Row

  • GRK

    I found a possible solution:

    • MyDataGridView.AllowUserToAddRows = True
    • Unhook the AddNewItem button:
      MyBindingNavigator.AddNewItem = Nothing
    • In the event handler for the AddNewItem button, select the New Row:
      MyDataGridView.CurrentCell = MyDataGridView.Rows(MyDataGridView.NewRowIndex).Cells(1)

    Now, clicking the Add New button on the BindingNavigator selects the New Row, and the DataGridView.DefaultValuesNeeded event can be used.


  • Shodin

    how to unhook AddNewItem of BindingNavigator in C#

  • palmqvist

    Awesome. Work's good for me so far.

    -andy



  • DataGridView, BindingNavigator, and New Row