473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Data binding problem

Hello. I'm having trouble getting a combobox bound to a property of an
object. I am populating the combobox using data binding, but I can't
get the SelectedValue property to bind to an object; it never gets
updated. I've included my short sample below, it's from a form with just one
control, a combobox named "uxChoice". The line of code giving me grief
is: uxChoice.DataBindings.Add("SelectedValue", model, "SomeVariable")

Public Class Form1
Public Class anEntry
Public Sub New(ByVal objectId As Integer, ByVal description As
String)
_objectId = objectId
_description = description
End Sub

Private _objectId As Integer
Public ReadOnly Property ObjectId() As Integer
Get
Return _objectId
End Get
End Property

Private _description As String
Public ReadOnly Property Description() As String
Get
Return _description
End Get
End Property
End Class

Private listOfChoices As anEntry() = {New anEntry(1, "A"), New
anEntry(2, "B"), New anEntry(3, "C")}

Public Class SomeModel
Private _someVariable As Integer
Public Property SomeVariable() As Integer
Get
Return _someVariable
End Get
Set(ByVal value As Integer)
_someVariable = value
End Set
End Property
End Class
Private model As New SomeModel

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
uxChoice.DataSource = listOfChoices
uxChoice.DisplayMember = "Description"
uxChoice.ValueMember = "ObjectId"
uxChoice.DataBindings.Add("SelectedValue", model,
"SomeVariable")
End Sub

Private Sub uxChoice_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
uxChoice.SelectedIndexChanged
If uxChoice.SelectedValue IsNot Nothing Then
MsgBox(uxChoice.SelectedValue.ToString)
End If
MsgBox(model.SomeVariable.ToString)
End Sub
End Class
Mar 21 '07 #1
5 1441
Remove the databindings line, you don't need it after setting the
datasource manually. Use one or the other, not both. Also it is
preferred to use the SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) event with a combobox control, assuming
you are using .net 2.0

Mar 21 '07 #2
The datasource is used to create the list of choices in the combobox; the
databindings line is to bind the selected value to a property of an object,
two separate things, I don't see how removing the databindings line is going
to help anything.
---scott

"Charlie Brown" wrote:
Remove the databindings line, you don't need it after setting the
datasource manually. Use one or the other, not both. Also it is
preferred to use the SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) event with a combobox control, assuming
you are using .net 2.0

Mar 21 '07 #3
On Mar 21, 11:06 am, sthom...@phmining.com
<sthomasophmining...@discussions.microsoft.comwrot e:
The datasource is used to create the list of choices in the combobox; the
databindings line is to bind the selected value to a property of an object,
two separate things, I don't see how removing the databindings line is going
to help anything.
---scott

"Charlie Brown" wrote:
Remove the databindings line, you don't need it after setting the
datasource manually. Use one or the other, not both. Also it is
preferred to use the SelectionChangeCommitted(ByVal sender As Object,
ByVal e As System.EventArgs) event with a combobox control, assuming
you are using .net 2.0- Hide quoted text -

- Show quoted text -
To bind the selected value to another control, use the second
controls .DataBindings Collection

If you are binding the selected value of one combobox to a textbox
name MyTextBox

MyTextbox.DataBindings.Add("Text", uxChoice, "SelectedValue")

Mar 21 '07 #4
I'm not trying to bind to another control; as I mentioned earlier I'm trying
to bind to the property of an object, see the example.
---scott

"Charlie Brown" wrote:
To bind the selected value to another control, use the second
controls .DataBindings Collection

If you are binding the selected value of one combobox to a textbox
name MyTextBox

MyTextbox.DataBindings.Add("Text", uxChoice, "SelectedValue")
Mar 21 '07 #5
Sorry for the misunderstanding.

The DataBindings collection of a control will only bind that controls
properties to that of another object, it doesn't work in reverse. In
order to do this the way you would like, you would have to create a
bindable class either by implementing IBindableComponent or something
similar. The simplest solution would be to set the model instances
properties in the SelectionChangeCommited event. Whether or not that
will work for you depends on your ultimate goal.

Here is some ref to the interface

http://msdn2.microsoft.com/en-us/lib...component.aspx
Mar 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
3
by: vinayak | last post by:
Hi I am displaying data in Datagrid in ASP.NET with Edit/Update functionality for each row. On the same page I have 2 Button controls which submits the request to server. These button controls...
6
by: Brad | last post by:
I have a web service which returns a collection of class object (see below). I want to consume this service in another web application by binding it to a List control The data returns from the...
4
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems,...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
2
by: Matthias | last post by:
Hi Team this may be a newbie question. I have searched the discussions before posting: I'd like to re-use a form instance to edit record details. My data binding (a DataTable bound to...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
3
by: Max | last post by:
Hello, I made a windows form with a combo box and 4 text boxes. All 5 objects should get their data from a data set which is populated in the form load method. The combo box has item ids. When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.