Bindingsource AddingNew causes "Cannot add external objects to this list"
Hello!
I want some columns to be prefilled before added so I handled the AddingNew event of my bindingsource:
| | Dim _viewAs System.Data.DataView =New System.Data.DataView(OrderTable) Dim _rowAs 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?
[1171 byte] By [
TAS] at [2007-12-16]
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.
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 |
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?