Solved

Simple data binding at run-time

Posted on 2010-11-27
19
462 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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