Solved

Simple data binding at run-time

Posted on 2010-11-27
19
459 Views
Last Modified: 2012-05-10
I'm trying to bind a specific property of item in a collection to a specific property of a control. For some reason I can't find any examples on how to do this.

Say I have a small collection of 10 items and I want to bind the .SomeText property of item 2 in the collection to the .Text property of a Textbox so that when I change the contents of the item property the TextBox updates and vice versa.
0
Comment
Question by:ShaunDumas
  • 9
  • 6
  • 2
  • +2
19 Comments
 
LVL 21

Expert Comment

by:tovvenki
ID: 34228184
Hi,
the best approach is to make your collection as ObservableCollection and then bind so that if anything changes in the collection u can update the view and vice versa
For more info on binding using a ObservableCollection a check this
http://msdn.microsoft.com/en-us/library/ms748365.aspx

I hope that this helps you

Thanks and regards,
Venki
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34228729
0
 

Author Comment

by:ShaunDumas
ID: 34236810
samirbhogayta: I actually already looked at that example, it doesn't show me a code example of how I can bind to a specific item in a collection. I can bind to a stand-alone object, but how the heck do I tell the binding which item in a collection to bind to???
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ShaunDumas
ID: 34236817
tovvenki: the observable collection looks very interesting, but I'm not quite sure how I would get the working with the binding? I would still need to tell the binding what specific item to bind to???
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34237353
I dont think you can do that. You would have to use the binding navigator to navigate to a specific index in the collection.
0
 

Author Comment

by:ShaunDumas
ID: 34242673
Hmm, now I'm a little perplexed, how would I do that?

All the examples I have seen on the net are showing how to bind one static object with another, not an item in a collection. If I need a binding nagivator that's fine, but how do I do that?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34242695
0
 

Author Comment

by:ShaunDumas
ID: 34245776
I get the concepts of the binding source and navigator, but in my case I need to bind multiple items to different static objects, i.e. item 3 to a textbox, item 8 to a combobox, etc. It looks like I will need a binding source per binding!? Is there a way to bind multiple items in a collection to different objects, without using a binding source for each binding (wouldn't be dynamic) ???
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34246531
You can not use the binding that way. You may want to look into INotifyPropertyChange

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx
0
 

Author Comment

by:ShaunDumas
ID: 34251448
I'm familiar with INotifyPropertyChanged, but it still doesn't solve the issue.

How else could this be accomplished? There must be a way to do this somehow.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34251459
Why do you need to do it? Describe the full scenario.
0
 

Author Comment

by:ShaunDumas
ID: 34251576
It's quite simple, I have a lot of different objects that use a mixture of the same data. So I thought that instead of looping through all of them updating their properties, many of which use parts of the same data, it would be a lot simpler to make a collection that could hold the values, and I could just update the collection one time when needed, and all of the objects would automagically be updated. This needs to be dynamic, so I can't just create 40 odd static objects to hold the data for binding. That's why I wanted to use a list, but I could use anything that would allow me to do it dynamically. I'm guessing that I need some kind of container to hold the bindings.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34251614
I am not sure I understand. Any example?
0
 

Author Comment

by:ShaunDumas
ID: 34253217
Let's try a simple example.

We have 9 textboxes, they are "paired" as the font property is the same on boxes 1-2-3 and 4-5-6 and 7-8-9 and we don't want to change the property value on all 3 in a pair, rather just update the value somewhere, once, and then automatically update anywhere it's needed. I can't just bind them together as the bindings can differ between the pairs.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34255908
In my opinion, you would have to work with INotifyPropertyChanged.
0
 

Author Comment

by:ShaunDumas
ID: 34278581
Anyone else?
0
 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 34308980
Try this
Public Class Form1
    Private myCol As New List(Of MyItem)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 10
            myCol.Add(New MyItem(i, "Display text " & i, "Binded text " & i))
        Next
        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        ComboBox1.ValueMember = "ID"
        ComboBox1.DisplayMember = "DisplayText"
        ComboBox1.DataSource = myCol
        TextBox1.DataBindings.Add("Text", myCol, "BindedText")
    End Sub

    Private Class MyItem
        Private _DisplayText As String = ""
        Private _BindedText As String = ""
        Private _id As Integer = 0

        Public Property DisplayText() As String
            Get
                Return _DisplayText
            End Get
            Set(ByVal value As String)
                _DisplayText = value
            End Set
        End Property
        Public Property BindedText() As String
            Get
                Return _BindedText
            End Get
            Set(ByVal value As String)
                _BindedText = value
            End Set
        End Property
        Public Property ID() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property
        Public Sub New()

        End Sub
        Public Sub New(ByVal newId As Integer, ByVal newDisplayText As String, ByVal newBindedText As String)
            _id = newId : _DisplayText = newDisplayText : _BindedText = newBindedText
        End Sub
    End Class

End Class

Open in new window

0
 
LVL 28

Assisted Solution

by:Ark
Ark earned 500 total points
ID: 34309044
If you need multiple textboxes then
        TextBox1.DataBindings.Add("Text", myCol(1), "BindedText")
        TextBox2.DataBindings.Add("Text", myCol(1), "BindedText")
        TextBox3.DataBindings.Add("Text", myCol(7), "BindedText")
        TextBox4.DataBindings.Add("Text", myCol(7), "BindedText")
0
 

Author Closing Comment

by:ShaunDumas
ID: 34313596
Ark, you rock! Beautiful solution, thanks a lot!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - TypeInitializer Error 25 32
VB.net Move a class from Solution Items to a project 2 33
Name Space error VS2015 1 23
MYSQL responding very slow 3 21
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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