PC Review


Reply
Thread Tools Rate Thread

Databinding Comboboxes

 
 
Mike
Guest
Posts: n/a
 
      17th Mar 2004
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!
 
Reply With Quote
 
 
 
 
William Ryan eMVP
Guest
Posts: n/a
 
      17th Mar 2004
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
"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databinding expressions are only supported on objects that have a DataBinding event jobs Microsoft ASP .NET 0 26th Sep 2007 12:54 AM
Databinding 2 comboboxes to the same Datasource ECathell Microsoft VB .NET 4 17th May 2006 07:03 AM
databinding for multiple comboboxes =?Utf-8?B?S2V2aW4=?= Microsoft C# .NET 1 2nd Jun 2005 05:00 PM
DataBinding for two comboboxes =?Utf-8?B?ZGF2aWQua2FvQG9wY28uY29t?= Microsoft C# .NET 5 26th Jan 2005 07:48 PM
Databinding Bible (REPOST FROM m.p.d.f.WindowsForms.Databinding) a Microsoft ADO .NET 1 16th Jul 2004 03:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:06 PM.