Combo Box Binding problems

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

I have created a form which sets up a dataview. The form views one
record at a time using a currencymanager. This works fine. All my
text boxes bind.

However I have a combo box which gets its lookup values from another
(Advertising) table (AdvertisingID, Advertising). This works
.. However, where it doesn't work as well is when the form
loads, the combo boxes should bind to the AdvertisingID field and
display the respective data.

When the form loads the combo box only displays a blank. If I
select the combo box all the values from the lookup table are there.
But why doesn't it display the value when the form loads.

Please help. This has been driving me nuts for three days already and
just cannot figure out what the problem is.

I'll post my code:-

Creating the dataview and currency manager:-






Richard
 
Hi,

Please post some code.

Ken
--------------
I have created a form which sets up a dataview. The form views one
record at a time using a currencymanager. This works fine. All my
text boxes bind.

However I have a combo box which gets its lookup values from another
(Advertising) table (AdvertisingID, Advertising). This works
.. However, where it doesn't work as well is when the form
loads, the combo boxes should bind to the AdvertisingID field and
display the respective data.

When the form loads the combo box only displays a blank. If I
select the combo box all the values from the lookup table are there.
But why doesn't it display the value when the form loads.

Please help. This has been driving me nuts for three days already and
just cannot figure out what the problem is.

I'll post my code:-

Creating the dataview and currency manager:-






Richard
 
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Sellers")
objDataView = New DataView(objDataSet.Tables("Sellers"))
objCurrencyManager = CType(Me.BindingContext(objDataView),
CurrencyManager)


Creating the bindings. This gets called on loading the form. I've
stripped the bindings of text boxes etc.

Dim strAdvertising As String = "SELECT AdvertID, Advertising " & _
"FROM AdvertisingMedium"

cboAdvertising.DataBindings.Clear()

cnnOTwo = New SqlConnection(strConnection)
cmdAdvertising = New SqlCommand(strAdvertising, cnnOTwo)
Dim arrAdvertising As New ArrayList

Try
cnnOTwo.Open()
drSQL =
cmdAdvertising.ExecuteReader(CommandBehavior.CloseConnection)

While drSQL.Read
arrAdvertising.Add(New Category(drSQL.GetByte(0),
drSQL.GetString(1)))
End While

drSQL.Close()

cnnOTwo.Close()

Catch exsql As SqlException
MsgBox(exsql.ToString, MsgBoxStyle.Critical, Me.Text)
End Try

With cboAdvertising
.DataSource = arrAdvertising
.ValueMember = "ID"
.DisplayMember = "Name"
.Add("SelectedValue", objDataView, "AdvertisingMedium")
End With


It all works fine. The only bit I'm trying to understand is why the
combo box does not display the value of the underlying field that it
is (or should be) bound to.
 
Hi,

Are Name and ID properties in the Category class?

Ken
-------------------
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Sellers")
objDataView = New DataView(objDataSet.Tables("Sellers"))
objCurrencyManager = CType(Me.BindingContext(objDataView),
CurrencyManager)


Creating the bindings. This gets called on loading the form. I've
stripped the bindings of text boxes etc.

Dim strAdvertising As String = "SELECT AdvertID, Advertising " & _
"FROM AdvertisingMedium"

cboAdvertising.DataBindings.Clear()

cnnOTwo = New SqlConnection(strConnection)
cmdAdvertising = New SqlCommand(strAdvertising, cnnOTwo)
Dim arrAdvertising As New ArrayList

Try
cnnOTwo.Open()
drSQL =
cmdAdvertising.ExecuteReader(CommandBehavior.CloseConnection)

While drSQL.Read
arrAdvertising.Add(New Category(drSQL.GetByte(0),
drSQL.GetString(1)))
End While

drSQL.Close()

cnnOTwo.Close()

Catch exsql As SqlException
MsgBox(exsql.ToString, MsgBoxStyle.Critical, Me.Text)
End Try

With cboAdvertising
.DataSource = arrAdvertising
.ValueMember = "ID"
.DisplayMember = "Name"
.Add("SelectedValue", objDataView, "AdvertisingMedium")
End With


It all works fine. The only bit I'm trying to understand is why the
combo box does not display the value of the underlying field that it
is (or should be) bound to.
 
Yes that's right!

Richard


Hi,

Are Name and ID properties in the Category class?

Ken
-------------------
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Sellers")
objDataView = New DataView(objDataSet.Tables("Sellers"))
objCurrencyManager = CType(Me.BindingContext(objDataView),
CurrencyManager)


Creating the bindings. This gets called on loading the form. I've
stripped the bindings of text boxes etc.

Dim strAdvertising As String = "SELECT AdvertID, Advertising " & _
"FROM AdvertisingMedium"

cboAdvertising.DataBindings.Clear()

cnnOTwo = New SqlConnection(strConnection)
cmdAdvertising = New SqlCommand(strAdvertising, cnnOTwo)
Dim arrAdvertising As New ArrayList

Try
cnnOTwo.Open()
drSQL =
cmdAdvertising.ExecuteReader(CommandBehavior.CloseConnection)

While drSQL.Read
arrAdvertising.Add(New Category(drSQL.GetByte(0),
drSQL.GetString(1)))
End While

drSQL.Close()

cnnOTwo.Close()

Catch exsql As SqlException
MsgBox(exsql.ToString, MsgBoxStyle.Critical, Me.Text)
End Try

With cboAdvertising
.DataSource = arrAdvertising
.ValueMember = "ID"
.DisplayMember = "Name"
.Add("SelectedValue", objDataView, "AdvertisingMedium")
End With


It all works fine. The only bit I'm trying to understand is why the
combo box does not display the value of the underlying field that it
is (or should be) bound to.
 
Hi,

I looked at you code again. In vs.net 2002, 2003, and 2005
there is no add method for the combobox. Where did that line come from?

Ken
 
Thank you for your help Ken. I see what you mean. I've just changed
this line to

With cboAdvertising
.DataSource = arrAdvertising
.ValueMember = "ID"
.DisplayMember = "Name"
.DataBindings.Add("SelectedValue", objDataView,
"AdvertisingMedium")
End With

But the only difference I can make is if I try to bind the combobox
before adding the Datasource, Valuemember and Displaymember
propertties, there is a blank in the combobox, and if I bind the the
combobox to the field afterwards (as shown) the form loads with the
first value in the combobox selected.

Still cannot get it to display the actual data in the field when the
form loads though!

Richard
 
Hi,

You are setting the datasource, displaymember, and valuemember.
You do not need the databindings.add line.

Ken
-------------
Thank you for your help Ken. I see what you mean. I've just changed
this line to

With cboAdvertising
.DataSource = arrAdvertising
.ValueMember = "ID"
.DisplayMember = "Name"
.DataBindings.Add("SelectedValue", objDataView,
"AdvertisingMedium")
End With

But the only difference I can make is if I try to bind the combobox
before adding the Datasource, Valuemember and Displaymember
propertties, there is a blank in the combobox, and if I bind the the
combobox to the field afterwards (as shown) the form loads with the
first value in the combobox selected.

Still cannot get it to display the actual data in the field when the
form loads though!

Richard
 
Hi Ken,

Sorry if I've confused things.

The datasource of the combobox is merely the lookup table that
provides the primary key and description values for the arraylist
which then populates the displaymember and valuemember properties
combobox.

Without the databindings.add line, there is nothing to bind the
combobox to the foreign key of the form's datasource.

Richard
 
Sorry Ken,

My I am just trying to load the value that is already in the form's
dataset to display in the combo box when the form loads. I've taken
that line out but it does nothing.

Richard
 
I've sorted this problem out now. I've got rid of the class I was
using and used a dataset/dataadapter method.

Much simpler.

Richard
 
Back
Top