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?
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!
So, show us the part of the oPO.Encumbrances class that handles the IList or IBindingSource implementation
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER