To enable a DataRow to keep a custom object, create a new class inheriting DataRow class.
The code below shows how to keep an object of a custom class named "VideoGame".
#region Custom DataRow
pubilc class VideoGameDataRow : DataRow{
private VideoGame vdoGame;
public VideoGameDataRow(DataRowBuilder rowBuilder) : base(rowBuilder)
// Default constructor.
}
public VideoGame GameObject{
set { vdoGame = value; }
get { return vdoGame; }
}
}
#endregion
You will also need a custom DataTable to handle this custom DataRow since the default DataTable can take only the default DataRow.
Override the NewRow method (NewRowBuilder()) to handle the custom DataRow.
#region Custom DataTable
public class VideoGameTable : DataTable{
protected override DataRow NewRowBuilder(DataRowBuilder builder){
return new VideoGameDataRow(builder);
}
}
#endregion
When used, cast a created row as the custom DataRow.
#region Usage
...
VideoGame game = new VideoGame();
VideoGameDataTable vdoGameTable = new VideoGameDataTable();
VideoGameDataRow newGameRow = (VideoGameDataRow)(vdoGameTable.NewRow());
newGameRow.GameObject = game;
...
#endregion
Note that you cannot create a new DataRow using the new keyword (DataRow row = new DataRow();).
This will generate an error saying that DataRow(DataRowBuilder) is inaccessible due to its protection level.
This applies to a custom DataRow which inherits from DataRow as well.
|