Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've run into an interesting problem binding a combo box with a Linq query. I'm sure I'm just not hitting on the right combination but it's got me frustrated and chasing my tail.

For example, I have a linq query that merely loads an entire table into the query (no where clause) for my flock of chickens.

VB
Private Sub frmBirds_Shown(sender As Object, e As EventArgs) Handles Me.Shown
       Dim BirdQuery = (From b In Main.db.Birds Select b)
       BindingSource1.DataSource = BirdQuery
   End Sub


I have a combo box showing that birds gender, and set the item collection to Male, Female, and Undetermined (it's pretty tough knowing a baby chick's gender) and the text is bound to the query's field. As long as I don't try to change anything, the bindings work fine. However, if I change something from say Undetermined to Female, the whole form changes to the data for the first record with Female. (the datasource pointer changes to that record) Also, it populates the item collection with all of the records. I might have hundreds of Males, Females and Undetermined. It's behaving like a pretty cool search/browse form with every field being a new search.

The designer generated code for this representative combobox is:

VB
'ComboBox1
'
Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.BindingSource1, "Gender", True))
Me.ComboBox1.DataSource = Me.BindingSource1
Me.ComboBox1.DisplayMember = "Gender"
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Location = New System.Drawing.Point(165, 150)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
Me.ComboBox1.TabIndex = 3


It seems to be only the comboboxes that cause a record change. The text boxes, and Trackbars seem to behave normally.

My question is what should I bind to only affect the text value, and not the list values for that combo box. Gender isn't the only combo box that has to be populated with various options. For most of these combo boxes, static list items are provided, and others will be provided through another query (not yet written) that will list other bird id's for parentage, flock assignments etc, however these list items aren't part of the original BirdQuery and aren't going to be modified.

As my original (unimproved) question hinted at, this is very simple code. The logic on the form hasn't been started yet because the form itself isn't behaving itself yet. I'm just beginning to learn Linq. In the past I would have manually coded the form without bindings and using objects to a SqlDataReader. I'm trying to teach myself more efficient ways, and writing code for my own personal use is one of the ways that I do that, as well as enjoy my hobby. Apologies for the vague original question.

What I have tried:

I've tried changing the bindings to just about every combination I can imagine, Display Member, Value Member, Selected Value Member. Nothing seems to affect the outcome at all.
Posted
Updated 27-Sep-19 8:54am
v3
Comments
Maciej Los 27-Sep-19 2:01am    
No code - no answers.
Note: we can't read in your mind or direct from your screen. Please, improve your question by providing the code you use.
[no name] 27-Sep-19 11:48am    
I get the impression you're expecting the combo box to do something it's not capable of. Your emphasis on combo box (which is usually a bit player to something else) and no mention of a "grid" or "navigator", etc. leads me to that conclusion.
ScottJohnson13483 27-Sep-19 11:53am    
No grid, just a data entry form showing one record at a time. Sorry I didn't clarify that. At one point I had a binding navigator on the form to browse but started having this problem and removed it. The problem kept going so it's not the issue as far as I can tell. None of it's code remains behind. Essentially I'd like the combo boxes to have a static dropdown list of valid selections, and only the actual value being bound. Unfortunately it populates the list with the results of the entire table for that field. I'm not sure if I can post a screenshot of the form.
[no name] 27-Sep-19 12:11pm    
Your combo box (the way you describe it) should have 3 "constants": male, female, undetermined. It would be used to assign a "valid" gender to a new record. Period.

The "query" loaded in the combo box (and whatever else) is "illogical" and "does not compute".
ScottJohnson13483 27-Sep-19 12:54pm    
Now you're where I am! *smile* There isn't a query for the constants although for some reason it's populating the combobox's items with the results of a query (even though I never bound the display member or value member). The only property that's bound to the query is the Selected Value... which as I understand it is what's displayed as text, and *should* be written back to the database if it's changed. Unfortunately when it's changed, the datasource pointer moves to a whole new (different, not new as insert) record, and drags the bound controls with it.

Sadly this is something that I can't send you code to reproduce--and see what I'm seeing-- unless you want me to email the entire solution and database. It's connected to a SqlExpress db on my local machine.

1 solution

Ok, I finally had some time to mess with it more. You actually pointed me in the right direction by stating that I was making it more complicated than it needed to be.

When I set up the databindings, I used the tool provided on the combobox control itself (the little arrow in the upper corner of the combobox in the designer). Apparently using that does things differently than using the databinding functions in the properties box.

In this case, I removed the existing combo box and started over, this time not using the controls tool, and setting up the binding for the "Text" property to queries datasource, and populated the list collection like I normally do in the properties box as well. This time I got the following in the generated designer code.

VB
'
       'ComboBox1
       '
       Me.ComboBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.BindingSource1, "Gender", True))
       Me.ComboBox1.FormattingEnabled = True
       Me.ComboBox1.Items.AddRange(New Object() {"Female", "Male", "Undetermined"})
       Me.ComboBox1.Location = New System.Drawing.Point(165, 150)
       Me.ComboBox1.Name = "ComboBox1"
       Me.ComboBox1.Size = New System.Drawing.Size(121, 21)
       Me.ComboBox1.TabIndex = 3


As you can see, this time it kept the Items.AddRange, and bound the Binding Source's "Gender" member to the Text property rather than the SelectedValue. I'm still not sure why the previous behavior took place though. At least this particular problem appears to be solved. Changing the gender on an existing record did not display a different record, and it did update the table when SubmitChanges ran.

I learned something new today, and I appreciate all the help. Sometimes it just takes a kick in the head to make me think simpler than I do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900