Solved

vb.net combobox binding

Posted on 2009-04-01
10
1,418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

733 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