locked
Bindingsource AddNew not working like the navigator button. RRS feed

  • Question


  • I have a simple typed Dataset with one table called person. The table has an ID, firstName, lastName column. The first and lastname columns are set in the dataset designer allowDBNull=false and their default value is <DBNull>.

    When I bind the dataset to a bindingsource and use the + button on the navigator it advances to a new row, bound text-boxes are hooked-up and allows me to populate it and save it.

    However, If I call bindingsource.AddNew() from my own button it never advances to this new row to let me populate it.

    If I set a breakpoint on the call to bindingsource.AddNew() I can see that:

    - The count is 1.

    - The current property is set to a detached row.

    - The firstName and lastName fields in that detached row have throw errors saying Threw an exception of System.InvalidCast Exception.

    Surely this is something simple I am doing wrong? I assumed calling AddNew() would show this new detached row automaticallyin the controls like the + button does.

    Any help is greatly appreciated!

    Saturday, April 21, 2018 5:55 PM

Answers

  • Figured it out. I had my controls bound to the dataset not the bindingsource. Word of warning to all reading. Don't write code when you are sleepy!
    • Marked as answer by ThomasTech Saturday, April 21, 2018 6:38 PM
    Saturday, April 21, 2018 6:38 PM