Hell = ComboBox databinding at design time

G

Guest

Hi there,

I'm using VS.NET 2003 / VB.NET and binding a strongly-typed dataset (Fln1)
to my combobox. The strongly-typed dataset is displayed in the component
"tray" on my form, so at design time, I can configure the combobox to bind to
Fln1, and the Windows Forms Designer generates the following code:

Me.ComboBox1.DisplayMember = "LOCTYPE.LocationType"
Me.ComboBox1.ValueMember = "LOCTYPE.LocationType"
Me.ComboBox1.DataSource = Me.Fln1
Me.ComboBox1.DataBindings.Add(New
System.Windows.Forms.Binding("SelectedValue", Me.Fln1, "FLN.LocationType"))

So this looks right... I thought!

In the Form_Load event of my Form, I fill up the dataset (Fln1) with the
data needed, and the form displays itself - but the combobox is not bound
correctly: it is positioned at the first item in the combobox, not the
correct one for the dataset contents!

BUT, if I take those EXACT same lines of code and move them from the "
Windows Form Designer generated code " section to the Form_Load event, the
combobox functions correctly - the right element in the Combobox is selected!

Aaaarrrgghhhh!?!?!?! Why? I would like to be able to configure this at
design-time, not waiting until run-time, does anyone have any ideas what I'm
doing wrong?

- David McClelland
 
T

Truong Hong Thi

Maybe you need to fill the dataset earlier; let's move it to the
constructor, before the call of InitializeComponent and see if it works.
 
G

Guest

Thank you so much for the suggestion; however, when I attempt to fill the
dataset before the call to InitializeComponent() the dataset has not be
instantiated yet, since this line is inside InitializeComponent():

Me.Fln1 = New WindowsApplication4.FLN

I tried moving this line out of InitializeComponent(), but then the Forms
designer got all upset saying "The variable 'Fln1' is either undeclared or
was never assigned."

So... I undid that change, and then I tried filling the dataset in the
constructor right after the call to InitializeComponent() - and it worked!

Thanks for pointing me down the right track!

- David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top