Avatar of pigparent
 asked on

DataGridView with BindingSource not updating dataset

I have a DataGridView (dgvEncumbr) that is bound to a business object dataset property (oPO.Encumbrances) by way of a BindingSource (bsEncumbr), as shown below under "Bind DataGridView".

When a user attempts to add a new object (oPO), I am resetting the binding to make sure there is no risidual from an earlier selection.  When the user clicks the save button (buttonSavePO), I am updating the BindingSource like so:  bsEncumbr.EndEdit().

The trouble is that it will not save the dgv entries until after the oPO object is saved once.  When I step through the code, I find that bsEncumbr.EndEdit has the new records from the dataset, but it isn't making it from bsEncumbr to the dataset in the business object.  This actually used to work and it stopped.  Am I missing some code that will update the business object (dataset) from the BindingSource?
'Bind DataGridView
bsEncumbr.DataSource = oPO.Encumbrances
bsEncumbr.DataMember = "POEncumbrances"
dgvEncumbr.DataSource = bsEncumbr
bsEncumbr.EndEdit()  'records are present at runtime
'code in the business object's Save() method
m_daEncumbr.Update(m_dsEncumbr, "POEncumbrances")  'records are NOT present in the dataset!

Open in new window

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon

FYI, I have tested this in debug mode and calling the .EndEdit method on the BindingSource for the datagrid view does not trigger the property Set on the business object.  In other words, the Binding Source is NOT updating the bound source on .EndEdit.

So, show us the part of the oPO.Encumbrances class that handles the IList or IBindingSource implementation

I don't know much about interfaces, so I hope this answers your question.  The oPO.Encumbrances property is a dataset.  The code below shows how it is declared, set, and saved in the business object (oPO).
    Dim m_daEncumbr As New OleDbDataAdapter
    Dim m_dsEncumbr As New DataSet("POEncumbrances")
    Property Encumbrances() As DataSet
            Return m_dsEncumbr
        End Get
        Set(ByVal value As DataSet)
            m_dsEncumbr = value
        End Set
    End Property
    Private Sub SaveEncumbrances()
        Dim cb As New OleDbCommandBuilder(m_daEncumbr)
            m_daEncumbr.SelectCommand = New OleDbCommand("SELECT * FROM " & _
                        "POEncumbrances WHERE PONo = '" & sPONo & "' ORDER BY LineNo, " & _
                        "EncDate", con)
            m_daEncumbr.InsertCommand = cb.GetInsertCommand
            m_daEncumbr.DeleteCommand = cb.GetDeleteCommand
            m_daEncumbr.UpdateCommand = cb.GetUpdateCommand
            m_daEncumbr.Update(m_dsEncumbr, "POEncumbrances")
        Catch dbex As OleDbException
            MessageBox.Show("Cannot connect to the database.  Check the network status and try again.", "Data access error")
        Catch ioex As InvalidOperationException
        End Try
    End Sub

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Thanks for your help.  I was already using the commandbuilder before the fact, and that actually wasn't the problem.  I finally discovered that one spot where I was reinitializing the business object was causing the problem.  I still don't see why, since the datagridview didn't even have data in it yet, but it was nonetheless the culprit and fixed it right up.  Your assistance led me to the solution, so you were awarded points.

I am still having another problem with the datagridview, but I will post that as another question.  It keeps adding the blank row at the end of the datagridview to my dataset and I can't figure out how to get it to stop.