Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I've a really serious problem,

I've a DataGridView control bounded to a custom entity collection that implements IListSource interface
I've a Data Browser form that contains a grid full of data from a specific table and this form is shown as a dialog from the previous form
When the user clicks Ok button, the selected rows from the browser grid should be added to the Grid on the caller form
I perform a For-Loop operation on selected rows to add them to the grid using this code

For Each row As DataBrowserRow In frm.SelectedRows
    'Set values for cells in the current row

    'Here is the code that produces the error where i'm trying to move to the next record
    If Me.DataSource IsNot Nothing AndAlso TypeOf Me.DataSource Is EntityCollectionBase Then
        Dim ds As EntityCollectionBase = CType(Me.DataSource, EntityCollectionBase)
        Dim entity As EntityBase = ds.CreateNewItem
        ds.Add(entity)  'This is the exact error line
        Me.CurrentCell = Me.Rows(Me.Rows.Count - 1).Cells(browser.TargetColumn)
    End If

This code raises the error:

Operation is not valid due to the current state of the object.

Stack trace:
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
at System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
at System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e)
at System.ComponentModel.BindingList`1.FireListChanged(ListChangedType type, Int32 index)
at System.ComponentModel.BindingList`1.InsertItem(Int32 index, T item)
at System.Collections.ObjectModel.Collection`1.Add(T item)
at BusinessLogicLayer.EntityCollectionBase`1.Add(EntityBase item)

I need to add a new row to the data grid anyway possible
Please this is an urgent issue, I hope to get help ASAP

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900