Solved

vb.net combobox binding

Posted on 2009-04-01
10
1,421 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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