A day with .Net

My day to day experince in .net

Implement INotifyPropertyChanged in Windows Forms and GridView

Posted by vivekcek on May 3, 2013

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

In the below example, we have a form that contain a gridview, which bind a list of Contact objects.

Contact Class
——————————

This class contain a public property called Name. This class also implement the interface INotifyPropertyChanged which is present in System.ComponentModel.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WinFormInotyFy
{
    public class Contact : INotifyPropertyChanged
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; OnPropertyChanged(this, "Name"); }
        }
              
        
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(object sender,string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender,new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Form Class
—————————

Our form contain a gridview. We use BindingSource class and BindingList for data binding purposes. BindingList is a part of System.ComponentModel.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace WinFormInotyFy
{
    public partial class Form1 : Form
    {
                
        BindingSource bSource = new BindingSource();
        BindingList<Contact> contacts = new BindingList<Contact>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {                        
            contacts.Add(new Contact() { Name="vivek" });
            contacts.Add(new Contact() { Name = "vivek" });
            contacts.Add(new Contact() { Name = "vivek" });
            contacts.Add(new Contact() { Name = "vivek" });
            contacts.Add(new Contact() { Name = "vivek" });
            bSource.DataSource = contacts;
            gridControl1.DataSource = bSource;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Contact c in contacts)
            {
                c.Name += "1";
              
            }
           
        }
              
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s