New Article: Extending the DataGrid using CodeDom
A
new article
that I wrote is published on the
MSDN Belux site. It's about
how to extend the DataGrid in the Windows.Forms namespace by
adding a custom DataGridColumnStyle to it. Just creating the
ColumnStyle is not that difficult, but when you want full
design-time support for that column (e.g. listing it in the
dropdown when adding ColumnStyles to a TableStyle), things
get a little bit complicated. This article shows how to
accomplish this and illustrates it with an example. The
example is the ExtendedDataGrid that has a new ColumnStyle;
the DataGridObjectColumn. This column can be used to
evaluate expressions entered at design-time and displaying
the results in the DataGrid. For example it's possible to
have a column in your grid that evaluates price *
(vat/100)without having to add another property to your
collection or a calculated column to the your DataSet. The
trick is to compile the expression that is stored as a
string, using CodeDom. This technique is explained as well
in the article.
Summary: The System.Windows.Forms DataGrid has several limitations, but is very extensible. This article shows how to create a custom DataGridColumnStyle that evaluates expressions for each row, stored in a string value, at run-time. This can be useful if the DataGrid shows rows that have properties that are objects themselves, or to add calculated fields. To obtain this behavior, an ExpressionEngine is built which can compile and execute code at run-time. To ensure ease-of-use and provide full design-time support for the custom DataGridColumnStyle, a custom DataGrid, based on the standard DataGrid is built too.
My thanks go to Tom, who managed to put the article online while attending the PDC! Btw, he's got an excellent coverage of this event on his blog.