locked
Bindingsource AddingNew causes "Cannot add external objects to this list" RRS feed

  • Question

  • Hello!

    I want some columns to be prefilled before added so I handled the AddingNew event of my bindingsource:


    Dim _view As System.Data.DataView = New System.Data.DataView(OrderTable)
    Dim _row As System.Data.DataRowView = _view.AddNew
    _row.Item("PositionsNr") = 4
    _row.Item("PositionsSubNr") = 1
    e.NewObject = _row

     


    While executing it raises the error "Cannot add external objects to this list". I think it's some kind of misunderstanding for my part. Could you please give me a clue?
    Tuesday, June 14, 2005 2:15 PM

Answers

  • You're close - the issue is two DataViews are getting created on top of the table and a row from one DataView is getting added to another DataView.  If you use the same DataView the BindingSource is using then this will work correctly (see below).

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


            Dim bindingSource As BindingSource = CType(sender, BindingSource)
            Dim view As System.Data.DataView = CType(bindingSource.List, System.Data.DataView)
            Dim _row As System.Data.DataRowView

            _row = view.AddNew()

            _row.Item("PositionsNr") = 4
            _row.Item("PositionsSubNr") = 1

            bindingSource.Position = bindingSource.Count - 1

            e.NewObject = _row
     

    Saturday, June 18, 2005 1:15 PM

All replies

  • The BindingSource.AddingNew event has an event args of type AddningNewEventArgs, which supports a NewObject property.  Define a new object, set the default values, and set the NewObject property to this new object.

     

    Wednesday, June 15, 2005 8:46 PM
  • This is exactly what I tried to do. As you can see in my code snippet I set e.NewObject to this created tablerow. The bindingsource is bound to this table. I think I have to create another object to get a new row for this bindingsource? But how?

    Thanks.
    Thursday, June 16, 2005 8:26 AM
  • You're close - the issue is two DataViews are getting created on top of the table and a row from one DataView is getting added to another DataView.  If you use the same DataView the BindingSource is using then this will work correctly (see below).

    Joe Stegman
    The Windows Forms Team
    Microsoft Corp.

    This posting is provided "AS IS" with no warranties, and confers no rights.


            Dim bindingSource As BindingSource = CType(sender, BindingSource)
            Dim view As System.Data.DataView = CType(bindingSource.List, System.Data.DataView)
            Dim _row As System.Data.DataRowView

            _row = view.AddNew()

            _row.Item("PositionsNr") = 4
            _row.Item("PositionsSubNr") = 1

            bindingSource.Position = bindingSource.Count - 1

            e.NewObject = _row
     

    Saturday, June 18, 2005 1:15 PM
  • Thank you. That was exactly the missing point!
    Tuesday, June 21, 2005 7:00 AM
  • Joe:  Many thanks - you provided the missing link for me, too.  I'm having to add a rowguid value to a new row, and this finally gave me the ability to do it.

    Everything seems to work just fine as long as I go to the last line  (the * line) in the DataGridView to initiate the new item operation.  A new rowguid gets assigned by my code.  But,  I can then cancel out without doing anything.  That is, if I just click on another row, the addition is cancelled.  Repeating the operation gets a new row again, and a new rowguid.  Great.

    If, however, I use the BindingNavigator's AddNewItem control which is connected to this BindingSource to initiate the operation, I get a new record with a new rowguid from my AddingNew code.  It's always inserted at the end of the datagrid ABOVE the * line.  And I cannot just escape out of if I chose not to add it.   I have to go ahead and at least fill in the data for those fields that don't allow null or I get a data error if I click elsewhere. 

    I have the sneaking suspiscion that this has something to do with BeginEdit(), but have not figured out when/where to do it.

    How do I get the same semantics that the DataGridView does?
       

    Tuesday, October 4, 2005 1:50 AM
  • the code doesnot work when the table is empty.I use guid pk column. why?A bug?
    Tuesday, June 6, 2006 11:02 AM