Q: ValueObject design pattern and DataGrid control

G

Guy Mahieu

Hello all,

I've been developing enterprise solutions in Java for a few years now, and I
became quite curious about .NET. When the opportunity arose to start a new
(relatively small) project, I chose to give c# a try.

I am used to implementing a ValueObject pattern when it comes to retrieving
data from a datasource and showing it in a grid (table) or form view. I was
curious about the best way to go about this in dotnet using the DataGrid
component, can it display custom objects as rows and how do you bind columns
to properties or fields of those row objects? What are the do's and don'ts
when it comes to adding data to and editing data in a DataGrid component?

The application I am trying to put together is a single tier application
deployed on a few workstations that each connect to a (mysql) database on a
server.

Thanks in advance for your hints, comments, ...

Greetings,

Guy
 
D

Dave Foderick

Hi Guy,

..NET can databind to custom objects as long as they have public properties
(cannot bind to fields). The collection that holds your objects should
implement IBindingList or at least IList. Where you will run into problems
is binding to null value properties like DateTime. In that case you may have
to create a subclass of the Binding class and override the OnFormat and
OnParse methods. Also for sorting look at the IComparer interface. Hopefully
databinding to custom objects will get easier with .NET 2.0

-Dave Foderick
 
G

Guy Mahieu

Thanks, this seems to work just fine, there is one drawback though, now all
my properties are exposed to the grid and the columnheaders show the propety
names as they were set in the class. What would be the best way to
customize the visible columns and their headers without altering the
underlying value object classes?

Greetings,

Guy
 
C

ClayB [Syncfusion]

To control the columns and their order in the DataGrid, you can add a
DataGridTableStyle to the DataGrid's TableStyles collection. In this
tablestyle, add columnstyles to the tablestyle1.GridColumnStyles for each
column you want to see. Here is a FAQ discussing this process for a
DataTable, but it will also work for other datasources.

How can I programatically add and remove columns in my DataGrid without
modifying the DataTable datasource?
http://www.syncfusion.com/faq/winforms/search/1070.asp

One point to note is the MappingName you assign to the tablestyle is
crucial. If it in not the proper value, the grid will not use the
tablestyle. Here is a FAQ that has a method that you can use to determine
the string you must use for the MappingName.

How do I determine the DataGridTableStyle MappingName that should used
for a DataGrid to make sure the grid uses my tablestyle ?
http://www.syncfusion.com/faq/winforms/search/931.asp

=============================================
Clay Burch, .NET MVP

Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top