Search code examples
winformsc#-4.0datagridviewdatagridviewcolumn

Is it possible to derive a DataGridView Column such that it can store an object of any sort?


I have a DataGridView that loads information from files and stores that information. The information is kept in a class form for use so I was thinking rather than have to reconstruct that class object from the data stored in the DGV, it would be easier to just save the class object into a column and access that object from that column when necessary. Is that possible? Would it have to do with DataBinding?

So ultimately what I would like is to be able to is this:

DataGridView DGV = new DataGridView;
//Initialization Stuff

DGV.Rows[0].Columns["objectColumn"].value = <ANY OBJECT OR CLASS TYPE AT ALL HERE>

Of course the column would be hidden.


Solution

  • DataGridViewCell.Value is of type Object which means you can store any object inside it. If your class overrides ToString method and provides a meaningful implementation it will be displayed in the respective cell.

    If you don't override the ToString you can still use DataGridView.CellFormatting event to format your object.