Solved

Simple data binding at run-time

Posted on 2010-11-27
19
456 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:ShaunDumas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:ShaunDumas
Comment Utility
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
Comment Utility
You can not use the binding that way. You may want to look into INotifyPropertyChange

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ShaunDumas
Comment Utility
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
Comment Utility
Why do you need to do it? Describe the full scenario.
0
 

Author Comment

by:ShaunDumas
Comment Utility
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
Comment Utility
I am not sure I understand. Any example?
0
 

Author Comment

by:ShaunDumas
Comment Utility
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
Comment Utility
In my opinion, you would have to work with INotifyPropertyChanged.
0
 

Author Comment

by:ShaunDumas
Comment Utility
Anyone else?
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Ark, you rock! Beautiful solution, thanks a lot!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

11 Experts available now in Live!

Get 1:1 Help Now