Solved

vb.net combobox binding

Posted on 2009-04-01
10
1,415 Views
Last Modified: 2012-05-07
I have a winform with a datagridview and a combobox.  In the form_load event I create a dataset table and then set it to the combobox's datasource.   When I move from datagridview row to row, the combobox.text stays in synch just like it should .. the selectedvalue field is in the same datasource as the datagridview's datasource.  The problem is when I change the datasource of the combobox, the combobox displaymembers and valuemembers change as they should per the coding, but the combobox does not automatically synch to the displaymember (based on the selectedvalue matching an matching valuemember in the combobox list).

So my questions are:
a) Why does the synching work in the form_load event automatically?
b) Why does changing the datsource cause the synching to not work and how do I fix the synching?
c) Can I reference the combobox's pre-datasource change 'selectedvalue' (but the combobox.text is null because the selectedvalue cannot find a matching valuemember thus the reason for needing to update the combobox datasource)
0
Comment
Question by:kperelman
  • 5
  • 5
10 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24049165
I could not really understand what you mean. Could you please explain what you are trying to do especially in terms of functionality and in terms of coding.
0
 

Author Comment

by:kperelman
ID: 24049949
CodeCruiser:

Sorry about the ambiguity .. let me try again.  

Basically as I move from row to row in the DGV the combobox list of values needs to change (its dependent on the dgv row data).  

For example, I have a dgv with 2 columns (column1 and column2) and 3 rows.  The column1/column2  values in the rows are (1,11), (2,12) and (3,13).  The combobox selectedvalue is bound to field2 (the same field2 that the dgv uses .. same datasource).  

When I enter each dgv I need to update the combobox displaymember/valuemember list. So I load the dataset datatable with displaymember/valuemember pairs of (ItemC,13), (ItemB,12) and (ItemA,11) and update the combobox displaymember, valuemember and datasource.

I click into the dgv row 1 column1 cell.  The problem is the combobox valuemember of 11 is not binding to field2's selectedvalue of 11.  The combobox.text shows the first entry (ItemC) in the list which is not correct .. it should be ItemA.  If I manually correct the combobox selected value for row1 to ItemA and then go to row2, the combobox shows ItemB .. and then go to row3, the combobox shows ItemC .. all works again.

It's like I need to tell the combobox to refresh its bindings after I update the combobox displaymember/valuemember list?

Is this any clearer?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24050174
How do you bind the combobox? Did you use the property window to set the databindings?
0
 

Author Comment

by:kperelman
ID: 24050717
CodeCruiser:

Yes I used the property window in the IDE when I created the combobox.  I did not touch it in the code.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24050752
What property you have bound and how? May be doing it in the code would help.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kperelman
ID: 24051246
CodeCruiser::

I am not sure how to do this in code?

a) Should I use combobox.databindings.add()
b) If yes, should I do a combobox.databindings.remove() or a combobox.databindings.clear()
c) Where should I place (a) .. before or after the setting the combobox.datasource?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24051547
Did you say that you want the combobox to change when you click on a different row in the datagrid?
0
 

Author Comment

by:kperelman
ID: 24052614
CodeCruiser:

Yes
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24057877
I think you would load the combobox once and then write code against the selection change event of the grid to manually select a different value in the combobox instead of binding it to the grid.
0
 

Author Closing Comment

by:kperelman
ID: 31597476
Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

26 Experts available now in Live!

Get 1:1 Help Now