Solved

ComboBox code with databinding

Posted on 2014-01-27
21
329 Views
Last Modified: 2014-02-08
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
Comment
Question by:zipnotic
  • 13
  • 6
  • 2
21 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39813612
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
 

Author Comment

by:zipnotic
ID: 39813775
I don't have a binding navigator on this form.  User chooses the record by clicking in a datagridview.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39813778
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
 

Author Comment

by:zipnotic
ID: 39813789
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
 

Author Comment

by:zipnotic
ID: 39813794
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
 
LVL 35

Expert Comment

by:YZlat
ID: 39813795
Can you post the code that you use in OnChange event for that combobox
0
 

Author Comment

by:zipnotic
ID: 39813831
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
 

Author Comment

by:zipnotic
ID: 39814166
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39814723
Have you tried the code that I supplied in http:#a39813612 ?
0
 

Author Comment

by:zipnotic
ID: 39814867
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39814921
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
 

Author Comment

by:zipnotic
ID: 39814928
Yes.
0
 

Author Comment

by:zipnotic
ID: 39814940
And have that value stored in alerts2 table column "AlertType"
0
 

Author Comment

by:zipnotic
ID: 39815205
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39815480
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
 

Author Comment

by:zipnotic
ID: 39815496
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816319
dAlerts2 is the datatable which is bound to the grid? How do you add the record to the grid?
0
 

Author Comment

by:zipnotic
ID: 39816345
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816461
Try changing

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

to

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

Accepted Solution

by:
zipnotic earned 0 total points
ID: 39818010
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
 

Author Closing Comment

by:zipnotic
ID: 39843893
Was able to find the solution on my own.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now