Responding to a property change event

I tried following this example (http://stackoverflow.com/questions/2246777/raise-an-event-whenever-a-propertys-value-changed) but nothing fired so I just decided to implement the PropertyValueChanged event of the PropertyGrid control. That works but what I need to do is get the actual instance and property of the class that is being affected. Anyone know how I can do this via the event implementation?

        void propertyGrid1_PropertyValueChanged(object s, System.Windows.Forms.PropertyValueChangedEventArgs e)
        {
            // What do I put here to get the instance of the bound object and the property being changed?
        }

Open in new window

LVL 22
Russ SuterSenior Software DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

themrrobertCommented:
e.ChangedItem is the GridItem that was changed.
e.OldValue is the oldvalue
e.ChangedItem.value is the new value
Russ SuterSenior Software DeveloperAuthor Commented:
I don't want the GridItem. I want the object that represents the actual property of the underlying class.
themrrobertCommented:
Well the Object "s" is the sender, which is the actual object that generated the event.

If that's not what you're looking for, then you haven't given us enough information
it_saigeDeveloperCommented:
At it's most basic, this is the pattern you would follow:
using System;
using System.ComponentModel;

namespace EE_Q28785802
{
	class Program
	{
		static void Main(string[] args)
		{
			Person bob = new Person();
			bob.PropertyChanged += OnPropertyChanged;
			bob.Name = "Bob";
			bob.ID = 0;

			Console.ReadLine();
		}

		private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			Console.WriteLine("The property {0} has changed for {1}", e.PropertyName, sender);
		}
	}

	class Person : INotifyPropertyChanged
	{
		[NonSerialized]
		private PropertyChangedEventHandler _propertyChanged;
		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			PropertyChangedEventHandler handler = _propertyChanged;
			if (handler != null)
				handler(sender, e);
		}

		private int _id = -1;
		private string _name = string.Empty;

		public int ID
		{
			get { return _id; }
			set
			{
				if (!value.Equals(_id))
				{
					_id = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("ID"));
				}
			}
		}

		public string Name
		{
			get { return _name; }
			set
			{
				if (!value.Equals(_name))
				{
					_name = value;
					OnPropertyChanged(this, new PropertyChangedEventArgs("Name"));
				}
			}
		}

		public override string ToString()
		{
			return Name;
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.