views:

540

answers:

3

Yesterday a colleague asked me how to display data in a grid where the built in data binding doesn't support what he wants to do for some of the columns.

Pleased to be able to help I explained all about the OnRowDataBound event and how you can hook into it and dynamically manipulate the cells in the row object to do pretty much what you want. Great. If you're working with asp.net.

Only problem it he's writing a winforms app and the DataGridView doesn't support OnRowDataBound! I couldn't believe it, but it's just not there.

So how the hell do the winforms guys manage this?

A: 

I'm not exactly sure, since I'm an ASP.NET guy too, but in winforms you work in a true stateful environment, rather than a "pseudo-stateful" (through viewstate) environment.

So because there's no need to to do everything (such as change cell styles etc) before everything is flushed to a Response, you can wait until the data has finished binding and then change each row as you see fit, you've still got your datasource as well, so just use it if you need to do anything more with it.

SirDemon
+1  A: 

The RowsAdded event is roughly equivalent, but you can run into performance issues using this event to access rows because of the shared memory state it uses. This is essentially a scheme where the DataGridView tries to minimize its memory footprint by sharing some state when possible between rows until you access one of the shared rows, which causes it to unshare state (memory and possible performance hit). A lot has been optimized away, but I've run into issues with using this event for one reason or another.

So if RowsAdded works for you with no degradation of performance, great. If not, you still have lots of options. You can derive from the DataGridViewCell and DataGridView to add custom cell (column) types to the grid. This is probably the most complicated way to customize the DataGridView... but it would allow pretty much any kind of cell manipulation you'd want.

The CellPainting event gives you a hook into the painting of a particular cell if you wish you paint something with GDI+ (System.Drawing) before display. There's also a CellFormatting event which gives you special event args, as well - but I've never used that one, so I'm not sure if the shared memory state issue applies (normally for the painting event it does not).

HTH, Richard

ZeroBugBounce
A: 

So because there's no need to to do everything (such as change cell styles etc) before everything is flushed to a Response, you can wait until the data has finished binding and then change each row as you see fit, you've still got your datasource as well, so just use it if you need to do anything more with it.

To make it clear, the datagridview is currently bound, displays exisitng bound columns. Now, I need to add new columns in the datagridview and then display some values from a dictionary object.

tried, this code, but no luck. Columns get added, but values are not displayed.

                    if (dvMain.Columns[key] == null)
                    {
                        dvMain.Columns.Add(key, key);
                    }
                    dvMain[key,i].Value = myDictionary[key];
I think this is more to do with the way the columns are being added. If the columns are added before the databind then it certainly works, although that is not to say that the columns cannot be added during the bind.
Chris Needham