Adding entities to bound DataGridView

Posts   
 
    
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 18-Apr-2010 19:18:09   

Hi,

I just quickly went through all threads that returned from my 'DataGridView bound' search and I did find the answer. So, I apologize if this sounds repeated. smile

I have an 'Invoice' Windows form. On the form I have placed a bound 'DataGridView' to represent the invoice details. Self servicing.

The bindings are as follows:

Control: EntityCollection Name: ecPOS

Control: BindingSource Name: bindSource DataSource: ecPOS

Control: BindingSource Name: bsPOSDetail DataSource: bindSource DataMember: POSDetail

All of this was done declaratively.

When I try to add a new POSDetail entity programmatically to the underlying main form data source, the DataGridView does NOT show the new entity. However, when I try to add the entity to the grid's data source, the grid will show the new entity but the main form data source will not.

Private ReadOnly Property CurrentEntity() As POSEntity
        Get
            'Return CType(Me.bindingNavigator.BindingSource.Current, POSEntity)
            Return m_Current
        End Get
    End Property

Private Sub tbSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbSearch.KeyDown

        If e.KeyCode = Keys.Enter Then

            If String.IsNullOrEmpty(Me.tbSearch.Text) Then
                Return
            End If

            Dim p As New InventoryItemEntity(Me.tbSearch.Text)
            If p.IsNew = False Then
                Dim pd As New PosdetailEntity

                With pd
                    .IntSort = 1
                    .IntLineNo = 1
                    .StrProductId = p.StrProductId
                    .DecSalesPrice = p.DecPriceLevel1
                    .StrUnitAbbreviation = pd.InventoryItem.UnitOfMeasurement.StrUnitAbbreviation
                    .DecQty = 1
                End With

                Me.CurrentEntity.Posdetail.Add(pd)
            End If

            UpdateTotals()
            Me.tbSearch.Text = String.Empty

        End If

    End Sub

Am I doing anything wrong? If so, where? Otherwise, how can I trigger the grid to refresh the data. The 'Refresh' method does NOT work.

Thanks in advance.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Apr-2010 20:23:09   

Please try this:

Control: EntityCollection Name: ecPOS

Control: BindingSource Name: bindSource DataSource: ecPOS

detailGrildView.DataSource : bindSource detailGridView.DataMember = POSDetail

Instead of using two bindingSources. Or, what might work as well is to add the new record to the detail bindingSource. I know, it should work the way you do, but that bindingSource is tricky sometimes.

David Elizondo | LLBLGen Support Team
Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 18-Apr-2010 20:36:11   

Been there, done that rage

When using only one datasource (the second is added by VS btw), the same thing happens. Fixed!!! Thanks buddy!!

When adding directly to second datasource, the first datasource does NOT 'see' the new added entity.

I'm baffled!! frowning

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 19-Apr-2010 11:01:12   

Am I doing anything wrong? If so, where? Otherwise, how can I trigger the grid to refresh the data. The 'Refresh' method does NOT work.

Did you try to re-bind the grid to the dataSource?

Bashar
User
Posts: 108
Joined: 11-Nov-2004
# Posted on: 19-Apr-2010 14:46:10   

Using the 'ResetBindings' method? Yes, I did, but nothing. I removed one of the binding sources and that seems to have solved the problem. But, I still don't understand why it didn't work in the first place.

Hope you have a solution.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 19-Apr-2010 15:30:53   

I'm not sure what was the problem. Could you please attach a simple repro solution based on Northwind.