Binding a List with DataGridView

DataGridView control for WinForms was introduced in .NET 2.0 and VS 2005. Its a pretty flexible control offering a lot more than DataGrid control in the previous version. Recently I was looking for a control which inherently support embedded control in its cells and DataGridView came up as a strong candidate. It has a native support of some basic embedded controls like TextBox, CheckBox etc. I explored it a little more and found it extremely easy and quick to program. My usage of this control is not much advanced which I describe here.

It has been built for displaying relational data so you can bind it to a DataTable from the database or an array of business objects. What I have done here is that I used a BindingList object which is also provided with .NET and is a useful variant of List object. The advantage of BindingList is that it can act as a sink of events while simple List object can’t.  It provides a PropertyChangedEventHandler which is called when a row is added/updated or deleted. So you simple bind this list to your DataGridView and both objects will remain synchronized. If you add/delete/update something to one the other object will simple pick it up from the event. Have a look at this:

private void MyForm_Load(object sender, EventArgs e) 
{
             FillUpDataGridView();  
             MyDataGridView.DataSource = Settings.MyBindingList; 
}

The method FullUpDataGridView reads some business objects from a data source into the BindList object and the next statement displays the data in the control. You also need to bind DataGridView columns to properties of these business objects to specify which properties are displayed and which are not and their respective order.

Binding properties with columns of DataGridView control

This image shows name of the propery FeedEnabled which has been bound with the column "Enabled". My business object looks like:

public class FeedViewRow : INotifyPropertyChanged
{
    private string _FeedUri;
    private bool _AutoUpdate;
    private bool _FeedEnabled;         // Parameterless constructor for serialization
    public FeedViewRow() {  }
    public FeedViewRow(string Uri, bool Auto, bool Enabled)
    {
        _FeedUri = Uri;
        _AutoUpdate = Auto;
        _FeedEnabled = Enabled;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public bool FeedEnabled
    {
        get { return _FeedEnabled; }
        set { _FeedEnabled = value; this.NotifyPropertyChanged("FeedEnabled"); }
    }
    public string FeedUri
    {
        get { return _FeedUri; }
        set { _FeedUri = value; this.NotifyPropertyChanged("FeedUri"); }
    }
    public bool AutoUpdate
    {
        get { return _AutoUpdate; }
        set { _AutoUpdate = value; this.NotifyPropertyChanged("AutoUpdate"); }
    }
    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

Keep a string property for TextBox and bool for CheckBox.  You object should implement the INotifyPropertyChanged interface.

After this you can simply [de]serialize the BindingList object. For serialization:

IsolatedStorageFile Storage = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream File = new IsolatedStorageFileStream(StorageFileName,
FileMode.Create, FileAccess.Write, FileShare.None, Storage);
XmlSerializer Serializer = new XmlSerializer(typeof(BindingList)); 
Serializer.Serialize(File, this);
File.Close();
Storage.Close();

This will write out an XML of the whole list. Simple!

 

Share this post : digg it! Facebook it! live it! reddit! technorati! yahoo!

Advertisements

3 thoughts on “Binding a List with DataGridView

  1. And don’t forget to call EndEditing() on your MyDataGridView control before you serialize the binding list. Otherwise you will miss the latest change (or changes in the last edited cell).

  2. Thank you for your article. I am trying to use an XML file as input to a bound datagridview. Seeing that I am somewhat new to .net, is there any way I can get a copy of your sample program in C#?
    Thanks,
    Mike

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