Solved

Any way to achieve Sticky reference?

Posted on 2004-09-25
6
200 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
  • 2
6 Comments
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
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
Comment Utility
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
Comment Utility
This is done by the CurrencyManager which is stored in the BindingContext on the Form.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now