We help IT Professionals succeed at work.

DataGridView with BindingSource not updating dataset

1,940 Views
Last Modified: 2012-06-21
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
 
'buttonSavePO_Click
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

Comment
Watch Question

Author

Commented:
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.

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

Author

Commented:
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).
'Declared
    Dim m_daEncumbr As New OleDbDataAdapter
    Dim m_dsEncumbr As New DataSet("POEncumbrances")
 
'Set
    Property Encumbrances() As DataSet
        Get
            Return m_dsEncumbr
        End Get
        Set(ByVal value As DataSet)
            m_dsEncumbr = value
        End Set
    End Property
 
'Saved
    Private Sub SaveEncumbrances()
        Dim cb As New OleDbCommandBuilder(m_daEncumbr)
 
        Try
            con.Open()
            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
 
        Finally
            con.Close()
        End Try
    End Sub

Open in new window

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.