Databinding Comboboxes

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I am attempting to bind several text and comboboxes on a form to
datatable. The binding appears to work for all the textboxes but not
for the comboboxes. Can somebody tell me what I am doing wrong? I bind
the fields in the Form Load event. Once the user has entered thier
data they push an Enter button which will run da(dt).Update. Again,
the binded textboxes are succesfully written to the database but he
comboboxes are blank??? Here is a simple version of the code I am
using.

'****************************************

cn = New AdoceConnection
cn.Open(db)
dt = New DataTable("tblTagging_Sample")

sql = "SELECT * FROM tblTagging_Sample"
da = New AdoceDataAdapter(sql, cn)
da.Fill(dt)

'Add a new record
Me.BindingContext(dt).AddNew()
'Move to last record
Me.BindingContext(dt).Position = Me.BindingContext(dt).Count - 1

' BIND THE FIELDS
txtPersonel.DataBindings.Add(New Binding("Text", dt, "Personel"))
cboRecorder.DataBindings.Add(New Binding("SelectedValue", dt,
"Recorder"))
txtLocation.DataBindings.Add(New Binding("Text", dt, "Location"))

'**************************************************

Note: I have seen some information suggesting that you need to specify
a Datasource, DisplayMember, ValueMember when binding Comboboxes and
Listboxes but in this case I am not binding to a datasource. Just
manually populating the combobox with the Items Collection property of
my comboboxes.

Thanks!
 
Mike:

I'm lost on the part about manually populating the comboboxes. Why not use
DataSource, DisplayMember and Valuemember?

From what it looks like here, unless you have some code walking the
datatable and adding a given value as you walk through Rows, I don't see
where you are doing the manual population. If you were, do you have
something like this:

foreach(DataRow dr in dt.Rows){
combBox.Items.Add(dr[ColumnIndex]);
}

If so, kindly post it so I can see what you are talking about, if not then
this should work too:
' BIND THE FIELDS
txtPersonel.DataBindings.Add(New Binding("Text", dt, "Personel"))
cboRecorder.DataBindings.Add(New Binding("SelectedValue", dt,
"Recorder"))
txtLocation.DataBindings.Add(New Binding("Text", dt, "Location"))

txtPersonel.DataSource = dt
txtPersonel.DisplayMember = "Personel"
txtPersonal.ValueMember = "Recorder"
txtLocation.DataSource = dt
txtLocation.DisplayMember = "Location"

If those are the field names, this should work for you.

Since you say this approach isn't what you want though, I'm probably
misunderstanding the question. But based on what I see, I don't see the
manual addtions. Let me know though if this won't work for you and If I
understand the problem better, I'll do my best to help.

Cheers,

Bill
 
Back
Top