views:

45

answers:

1

Hi,

I would like create a custom DataRow that will have -let's say- a propery called IsCheapest.

public class RateDataRow : DataRow
{
    protected internal RateDataRow(DataRowBuilder builder) : base(builder)
    {
    }

    public bool IsCheapest { get; set ;}
}

And I want to have a new DataTable that contains only *RateDataRow*s so that .NewDataRow() returns RateDataRow instance as a new row.

what should be the implementation on the class that extends DataTable?

Thanks,

A: 

The DataTable exposes a GetRowType virtual method, override this in a derived class. Any attempts to add a row of the wrong type will throw an exception:

class Program
{
    static void Main(string[] args)
    {
        MyDataTable t = new MyDataTable();
        t.Rows.Add(t.NewRow()); // <-- Exception here, wrong type (base doesn't count).
    }
}

public class MyDataTable : DataTable
{
    public MyDataTable()
        : base()
    { }

    protected override Type GetRowType()
    {
        return typeof(MyDataRow);
    }
}

public class MyDataRow : DataRow
{
    public MyDataRow()
        : base(null)
    { }
}
Adam