views:

367

answers:

1

I have written my own data type that is then used as the type for a column in a datatable. I am reading XML data from a file. After I call the ReadXML(file) method, all the cells have values except for the cells in the columns with my data type. Is there some operator I need to override so that ReadXML(file) can take the data from the file and make a MyDataType out of it? Also, when I try to sort the defaultview on this column, I get "Object reference not to an instance of an object." Thanks for any help.

+1  A: 

Yes you can create a column using a custom data type.

Assuming that you want a property to be of type Address:

var ds = new DataSet();
var table = new DataTable();
table.Columns.Add("Address", typeof(Address));

ds.Tables.Add(table);

If you want to be able to serialize your dataset (you cannot serialize a datatable), you have to ensure that your type Address is serializable.

There are multiple way to mark as class as serializable. The easiest is to use the SerializableAttribute. But I recommand that your Address class implements the IXmlSerializable interface. It may be a little harder, but you will avoid the versioning problem.

Pierre-Alain Vigeant