• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Simple data binding at run-time

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
ShaunDumas
Asked:
ShaunDumas
  • 9
  • 6
  • 2
  • +2
2 Solutions
 
tovvenkiCommented:
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
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
0
 
ShaunDumasAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ShaunDumasAuthor Commented:
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
 
CodeCruiserCommented:
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
 
ShaunDumasAuthor Commented:
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
 
CodeCruiserCommented:
0
 
ShaunDumasAuthor Commented:
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
 
CodeCruiserCommented:
You can not use the binding that way. You may want to look into INotifyPropertyChange

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx
0
 
ShaunDumasAuthor Commented:
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
 
CodeCruiserCommented:
Why do you need to do it? Describe the full scenario.
0
 
ShaunDumasAuthor Commented:
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
 
CodeCruiserCommented:
I am not sure I understand. Any example?
0
 
ShaunDumasAuthor Commented:
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
 
CodeCruiserCommented:
In my opinion, you would have to work with INotifyPropertyChanged.
0
 
ShaunDumasAuthor Commented:
Anyone else?
0
 
ArkCommented:
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
 
ArkCommented:
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
 
ShaunDumasAuthor Commented:
Ark, you rock! Beautiful solution, thanks a lot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 9
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now