Visual C++ DataGridView – data binding

DataGridView is a control introduced in .NET Framework version 2.0. displaying data in customizable grid.  The control can be used in any application developed on .NET Framework using any language (C++, C#, VB) based on CLR (Common Language Runtime).
The control can be customized through manipulating of DataGridView class properties. Some of them are: DefaultCellStyle, CellBorderStyle, GridColor, just to name a few.
That is the short introduction do DataGridView Control. However, what I would like to concentrate in this post is specifying a data source for the control. There are several ways introduced in MSDN documentation:

DataGridView::DataSource Property

What was needed to implement in an application was using as a datasource some collection of simple POCO’s (Plain Old C/C++ Object).

So going this way, a POCO is needed:

The collection of objects of the above class is returned from a simple DAO:

and bound to DataGridViewControl:

What is assumpted is that control has columns specified with DataPropertyName equal to the property from POCO from which value goes to the particular column.

As a result data grid with proper columns is displayed, hovewer it contains no data. DAO returns data, it is bound to grid’s data source but nothing is displayed. What is more, it works perfectly in C# and probably in VB (not tested by myself) but not in C++.

To make it work, Bindable (documentation: MSDN BindableAttribute Class) attribute is needed to be specified on each and every property which is bound to the data grid column. The proper POCO should look like this:

Now, everything works properly and data grid displays data from the bound collection.

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of
%d bloggers like this:
Skip to toolbar