• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

ComboBox code with databinding

Hello,

Beating my head against the desk here.

I'm trying to get a combobox on a winform vb.net 2010 to show the value in the datatable for each record and allow the user to change the selected item in that combobox to the list items only.  With my code when I choose an item from the drop down list (which populates from ALL records) the form jumps to that record.  I want the CB to show the current value and change the current record when selecting a new value NOT jump to a different record.
I'm guessing the datasource or whatnot needs to be set differently but I can't seem to locate an example of exactly what I want to do, though it seems like it would be pretty standard behavior?

Thanks for any help!


 

cboType.Items.Clear()

         cboType.Items.AddRange({"Type 1", "Type 2", "Type 3"})

        cboType.DataSource = dsAlerts.Tables("tAlerts2")
        cboType.DisplayMember = "AlertType"
        cboType.SelectedValue = "AlertType"
        cboType.DataBindings.Add("SelectedValue", dsAlerts.Tables("tAlerts2"), "AlertType")
        ' cboType.ValueMember = cboType.SelectedValue

Open in new window

0
zipnotic
Asked:
zipnotic
  • 13
  • 6
  • 2
1 Solution
 
CodeCruiserCommented:
You mean your binding navigator moves to that record?

Try following


 

cboType.Items.Clear()

         cboType.Items.AddRange({"Type 1", "Type 2", "Type 3"})

        Dim dAlerts As DataTable = dsAlerts.Tables("tAlerts2").Copy()
        cboType.DataSource = dAlerts
        cboType.DisplayMember = "AlertType"
        cboType.SelectedValue = "AlertType"
        cboType.DataBindings.Add("SelectedValue", dAlerts, "AlertType")
        ' cboType.ValueMember = cboType.SelectedValue

Open in new window

0
 
zipnoticAuthor Commented:
I don't have a binding navigator on this form.  User chooses the record by clicking in a datagridview.
0
 
YZlatCommented:
I am not sure I understand what are you trying to do here?

You have a windows form with a combobox, correct? And then ypu select a value from the combobox, your form gets populated, correct?
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
zipnoticAuthor Commented:
When I click down arrow to look at list items and click one of them the current record jumps to the one corresponding to that list item. If there was 20 list items it would jump to whichever you click on.
0
 
zipnoticAuthor Commented:
The combo box is meant to limit the choices for that field to the 5 or so choices provided I'm the drop down list
0
 
YZlatCommented:
Can you post the code that you use in OnChange event for that combobox
0
 
zipnoticAuthor Commented:
I have nothing for on change event.  I've been wondering if I need to treat the combo box as unbound and manually insert the selected value into the dataset?  And of course manually load it on each current record.
0
 
zipnoticAuthor Commented:
Here's a picture of the form in progress.  The arrow is the problem combo box.  The user shall select 1 of 5 choices in the combobox for the type of alert that this record will be.  The lower right datagridview is bound to the tAlerts2 table.  The user can click in the DGV to navigate or use the buttons to move forward or back.  Changes can be made in the DGV or text boxes for each record before clicking SAVE to update/insert/delete.  The upper right DGV will be a child table.

 The box should be:

 blank for a new record
Show alert type for existing records
Allow changing alert type for that record without launching to a different record
Only allow user to choose something from drop down box (with suggest append)


What it does now (unwanted behavior)
Doesn't show alert types typed in list from designer or programmatically placed in drop list
Shows alert types present in datatable
Clicking on one of the drop downs sets the form to display the record associated with that item in the drop list.

combo form ex
0
 
CodeCruiserCommented:
Have you tried the code that I supplied in http:#a39813612 ?
0
 
zipnoticAuthor Commented:
CodeCruiser,  

Sorry I was away from a computer and was responding on my mobile. I did try your suggestion. Line 10 cbotype.selectedvalue = "AlertType" kicks off an error.  

Error is: A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

The code is in the on load event of the form.  When I step thru it line 10 makes the IDE return to the fAlerts.show() command and the immediate window shows the above error.  The form stills loads.  The CB drop down list shows all the rows with the field "AlertType" from the datatable instead of "Type 1", "Type 2", "Type 3" from the list added programmatically or in designer.

Does my picture help to get across what I'd like to accomplish?
0
 
CodeCruiserCommented:
I just realized that you are adding items manually to the combobox as well as binding it. So what do you want to see in the combobox? Type 1, Type 2, Type 3?
0
 
zipnoticAuthor Commented:
Yes.
0
 
zipnoticAuthor Commented:
And have that value stored in alerts2 table column "AlertType"
0
 
zipnoticAuthor Commented:
This is what it should look like.  The combobox should be synced to the datagridview as they are both the same dataset/table just like the text boxes.  Except the user needs to be limited in the combo box to the choices in the list.

combo 3
0
 
CodeCruiserCommented:
What happens if you remove lines 7-10 from your original code snippet?

        cboType.DataSource = dsAlerts.Tables("tAlerts2")
        cboType.DisplayMember = "AlertType"
        cboType.SelectedValue = "AlertType"
        cboType.DataBindings.Add("SelectedValue", dsAlerts.Tables("tAlerts2"), "AlertType")
0
 
zipnoticAuthor Commented:
Removing lines 7-10 makes the combo box correctly have Type 1 etc.  In the drop list but it does not store the chosen value into the current record in the dataset table "tAlerts2".
0
 
CodeCruiserCommented:
dAlerts2 is the datatable which is bound to the grid? How do you add the record to the grid?
0
 
zipnoticAuthor Commented:
The grid is set to allow additions so I would type the new info into the grid or use the rest boxes above and require user to click NEW .  I haven't even begun creating INSERT functionaliy yet.
0
 
CodeCruiserCommented:
Try changing

cboType.DataBindings.Add("SelectedValue", dsAlerts.Tables("tAlerts2"), "AlertType")

to

cboType.DataBindings.Add("SelectedValue", dsAlerts.Tables("tAlerts2"), "AlertType", False, dataSourceUpdateMode.Never)
0
 
zipnoticAuthor Commented:
This did not produce desired behavior:

cboType.DataBindings.Add("SelectedValue", dsAlerts.Tables("tAlerts2"), "AlertType", False, dataSourceUpdateMode.Never)

BUT!

The Following settings seems to be doing what I expect:

Leave it UNBOUND
Datasource=Nothing
DisplayMenber = nothing
SelectedValue = nothing
AutoComplete=SuggestAppend
AutoCompleteSource=ListItems
Locked=True
ListItems added in Designer
CODE:
cboType.DataBindings.Add("SelectedItem", dsAlerts.Tables("tAlerts2"), "AlertType")

Just as simple as that it syncs with the dataset table AND Datagridview and updates properly to the database with a simple .update method.  I could prevent free typing by changing it to a dropdownstyle list but I don't care for the looks of that.

Thanks for the suggestions but I think I stumbled into the solution.
0
 
zipnoticAuthor Commented:
Was able to find the solution on my own.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 13
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now