I am getting what I consider an odd error when using a databound
combobox. I use 1 dataset with 4 tables, each filled with a different
data adapter. 3 of them are lookup tables which are bound to 3
different combo boxes. The 4th table is the main table (Equipment) of
data. I then bind many different textboxs to the Equipment table, as
well as the 3 comboboxes, a bound checkbox, and a bound
DateTimePicker. I can edit records and navigate the records with no
problem. Here is the weird part, when I create a new record using
Me.BindingContext(dsData, "Equipment").AddNew(), it blans all of the
fields out just like I would expect, I then set the comboboxes value
to 0 to make the first record the selected record:
Me.cboHost.SelectedValue = 0
Me.cboDepartment.SelectedValue = 0
Me.cboMount.SelectedValue = 0
When I attemp to save the record I call endcurrentedit:
Me.BindingContext(dsData, "Equipment").EndCurrentEdit() . I then get
an error that the field cboHost does not allow null. When I console
display the SelectedValue of the combobox, it says the value is 1. To
get around this, I can click in the combobox and change the value to
something else, then change it back, and no problem. What is the
reason why setting a SelectedValue at runtime in a combobox makes the
currencymanager think the value is null, and what can I do about it?
Here is the relevant parts of my code:
Private Sub BindFields()
txtID.DataBindings.Add("Text", dsData, "Equipment.ID")
cboHost.DataBindings.Add("SelectedValue", dsData,
"Equipment.HostID")
txtBuilding.DataBindings.Add("Text", dsData,
"Equipment.Building")
cboDepartment.DataBindings.Add "SelectedValue",dsData,
"Equipment.DepartmentID")
txtRoom.DataBindings.Add("Text", dsData, "Equipment.Room")
txtJack.DataBindings.Add("Text", dsData, "Equipment.Jack")
txtCable.DataBindings.Add("Text", dsData, "Equipment.Cable")
txtPair.DataBindings.Add("Text", dsData, "Equipment.Pair")
txtName.DataBindings.Add("Text", dsData, "Equipment.Name")
txtTN.DataBindings.Add("Text", dsData, "Equipment.TN")
txtNumber.DataBindings.Add("Text", dsData,
"Equipment.FullNumber")
txtPrefix.DataBindings.Add("Text", dsData, "Equipment.Prefix")
txtExt.DataBindings.Add("Text", dsData, "Equipment.Ext")
txtACD.DataBindings.Add("Text", dsData, "Equipment.ACD")
txtType.DataBindings.Add("Text", dsData, "Equipment.Type")
txtModel.DataBindings.Add("Text", dsData, "Equipment.Model")
cboMount.DataBindings.Add("SelectedValue", dsData,
"Equipment.MountID")
txtDES.DataBindings.Add("Text", dsData, "Equipment.DES")
txtRollup.DataBindings.Add("Text", dsData, "Equipment.RollUp")
txtVM.DataBindings.Add("Text", dsData, "Equipment.VM")
txtFDN.DataBindings.Add("Text", dsData, "Equipment.FDN")
txtHunt.DataBindings.Add("Text", dsData, "Equipment.Hunt")
txtVMRevert.DataBindings.Add("Text", dsData,
"Equipment.VMRevert")
txtNCOS.DataBindings.Add("Text", dsData, "Equipment.NCOS")
txtRNP.DataBindings.Add("Text", dsData, "Equipment.RNP")
txtSCC.DataBindings.Add("Text", dsData, "Equipment.SCC")
txtComments.DataBindings.Add("Text", dsData,
"Equipment.Comments")
chkInactive.DataBindings.Add("Checked", dsData,
"Equipment.Inactive")
dtpInactiveDate.DataBindings.Add("Text", dsData,
"Equipment.InactiveDate")
AddHandler Me.BindingContext(dsData,
"Equipment").PositionChanged, _
AddressOf UpdateStatusForPosition
End Sub
If Not IsDirty(dsData.Tables("Equipment")) Then
Exit Sub
End If
Private Function IsDirty(ByVal tbl As DataTable) As Boolean
Console.WriteLine(Me.cboHost.SelectedValue)
Me.BindingContext(dsData, "Equipment").EndCurrentEdit()
Dim dr As DataRow
For Each dr In tbl.Rows
If dr.RowState <> DataRowState.Unchanged Then
'it's dirty
Return True
End If
Next
'not dirty
Return False
End Function
If you need more information, please let me know. I am at witts end.
Thank you
Kalvin
Gary Shell - 13 Jul 2004 16:35 GMT
I noticed this same thing too. And I saw that the EndCurrentEdit was setting
the SelectedValue of the combobox to "nothing". The problem is that for some
odd reason during an Add New the underlying dataset item collection is NOT
being updated when a programmatic change is made to the SelectedValue
property. It does get updated if a manual selection is made in the combobox.
And it does get updated when a programmatic change is made to the
SelectedValue during an edit of an existing record.
The workaround is just prior to the EndCurrentEdit, set the dataset item
collection entry to the SelectedValue property for each combobox. Now the
EndCurrentEdit leaves the combobox SelectedValue property as is and the
database gets updated with the expected data.
I worked on this for two 14 hour days and my forehead and brick wall are
bloody to prove it. <grin>
Gary
> I am getting what I consider an odd error when using a databound
> combobox. I use 1 dataset with 4 tables, each filled with a different
[quoted text clipped - 95 lines]
> Thank you
> Kalvin