DataGridCell style override problem

Feb 12, 2009 at 11:00 AM
We are trying to get a styled WPF Toolkit Datagrid (not the windows forms datagrid).

I've searched the web and everything I've found suggests you take control of the cell style by utilising the loadingrow function and then getting the datagridcell.

Our grid is dynamically build from a datatable as we want to re-use the same style / colouring in several programs. The problem is the code...

DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row1);

always returns null as does either of the following...

FrameworkElement fex = colz.GetCellContent(dgrid2.Items[z2]);
FrameworkElement fe3 = col3.GetCellContent(row1);

where row1 is defined by;

DataGridRow row1 = (DataGridRow)dgrid2.ItemContainerGenerator.ContainerFromIndex(z2);

I feel this should be straightforward, but so far most things suggest hard-coding the column names and driving the style from however many converters are needed.

I can supply an example program showing the problem if needed, this is really stopping us from moving forward, which is frustrating as the answer seems just out of reach.

If anyone can help I'd be extremely grateful.

-Colin H
Feb 13, 2009 at 8:26 AM
Are you simply trying to style the cells? Have you seen the DatagridCellStyle property?

I would recommend reading this blog post:

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples

Feb 13, 2009 at 9:27 AM
Yes, I'm trying to colour the cells based on their data.
I can achieve a lot via converters but then I seem to be stuck with having a converter detect date fields to change the date display on auto-generated columns, indeed, I'm not even sure that is possible. That is to say, having a date converter automatically be called without implicitly defining the column name in the XAML, I've certianly not seen anything so far that has done that.

Thanks for the link,  I hadn't come across that one before but alas it still doesn't give me what I need, I am beginning to suspect I'll have to build the grid completely using templated columns. It would of been nice to let the datagrid auto-generate and just react via converters, it just seems it *should* be possible, (does not mean it is of course).
Feb 13, 2009 at 9:37 AM
Perhaps you could provide a little example of what you have so far? That woudld woudl certainly help us to understand your problem better?

Colin E.
Feb 13, 2009 at 10:59 AM
I'll extract the code into a simple version I can post a URL to, at present, it's chock full of various tests etc.. as I've tried to figure it out which would muddy the waters somewhat.

Please bear with me, but I'll hopefully get it up in the next couple of hours as I have to juggle it with my workload.
Feb 13, 2009 at 2:53 PM
I've managed to put the program into a 'simple' form and put the solution/project up on Mediafire:

The comment at the top of the '.cs' file should help explain matters a bit and you can see how I've gone about constructing my grid / converters.
As you may gather, the 'getmytable' function would normally represent a call to get a specific data table / query (in my case from an SQL server so it returns a datatable).

Colin H.