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 s
pecify which properties are displayed and which are not and their respective order.
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 : |


2 responses so far ↓
hashfactor // February 5, 2009 at 12:31 pm
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).
Rennie // March 15, 2009 at 12:34 pm
Thanks for this information. It ended several hours of me searching for “the missing link”.