Solved

Any way to achieve Sticky reference?

Posted on 2004-09-25
6
207 Views
Last Modified: 2010-04-15
Here is an example to hopefully help clarify what I'm trying to do.  

I load a collection with objects (TestClass).  I then bind this collection to a datagrid.  I then also bind text a text box to the current row in the gird like this:
grdList.DataSource = myCollection;
_refClass = (TestClass)myCollection[grdList.CurrentRowIndex];                  

txtRefName.DataBindings.Add("Text",_refClass, "Name");

The obviously the variable _refClass is just acting as a pointer which holds a reference to the item in the collection and then that reference is bound to the textbox.  So if in the future _refClass is set to another item in the collection the TextBox will not reflect that change unless it is rebound.

However, what I'd like to know is, is there a way actually bind the textbox to _refClass so that any time _refClass is pointed to a new item in the collection the TextBox will reflect this without having to be rebound.

The scenerio above isn't a real project I'm working on, I just thought it was the easist way to explain what I am looking for.  I have also coded this into a simple project as an example.  If it will help anyone to understand the issue I can email the project to them.

Thanks so much for your time!
0
Comment
Question by:carpediem
[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
  • 2
6 Comments
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12151123
Try:
txtRefName.DataBindings.Add(new Binding("Text", myCollection, ""));
PS: It's not tested but I'm sure you have to bind to the collection. The CurrencyManager on the form will handle the row changing thing.
0
 
LVL 1

Author Comment

by:carpediem
ID: 12151159
Sorry, my example wasn't very clear (or was maybe to clear and thus hid my real question).

In more generic terms:
Say there is an object X in memory and then object Y just holds a reference to object X.

I then set object Z = object Y.  So now object Z just points to object X.  

What I would like to know is there any way in C# to handle a case where I now point object Y to object Q and I would like object Z to automatically point to object Q as well.

So instead of having object Z directly point to object X when I set object Z = object Y, object Z actually points to object Y so if I point object Y elsewhere object Z will automatically then point to that other object I've updated object Y to point to.

0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 500 total points
ID: 12152333
This is done by the CurrencyManager which is stored in the BindingContext on the Form.
0

Featured Post

Technology Partners: 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…

762 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