Solved

Simple data binding at run-time

Posted on 2010-11-27
19
457 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 27

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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now