Wednesday, April 11, 2012

Inheriting from a DataRow


I needed to implement a custom data row in one of my projects which made me result to the following code.
Create a custom class which inherit from a DataRow. Add a constructor which accepts a DataRowBuilder.

public class CustomDataRow : DataRow {
   internal CustomDataRow(DataRowBuilder rowBuilder) : base(rowBuilder) { }

}

Override the NewRowFromBuilder method. Inside this method, call the CustomDataRow's constructor passing in the DataRowBuilder.

public class CustomDataTable : DataTable {
   public CustomDataTable(){ }

   protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {

      return new CustomDataRow(builder);

   }
}

This will enable each row created by the CustomDataTable to be of CustomDataRow.