DataRow class derivation

A

André Fereau

Hi,

I wish to use a class derivated from DataRow. The rows within a table are
created with the NewRow method of the DataTable class. So I writed a method
NewSTMTTRNRow() in my class STMTTRNDataTable, but I have an Invalid Cast
Exception in the code "return
((STMTTRNRow)(this.NewRow()));". Where is the bug? What is the smart way to
achieve my goal?

Code :

public class STMTTRNDataTable : DataTable, System.Collections.IEnumerable
{
public STMTTRNRow NewSTMTTRNRow()
{
return ((STMTTRNRow)(this.NewRow()));
}
public System.Collections.IEnumerator GetEnumerator()
{
return this.Rows.GetEnumerator();
}
}

public class STMTTRNRow : System.Data.DataRow
{
private STMTTRNDataTable tableSTMTTRN;
internal STMTTRNRow(DataRowBuilder rb) : base(rb)
{
this.tableSTMTTRN = ((STMTTRNDataTable)(this.Table));
}
}

Ragards,

AFer92
 
N

Nicholas Paldino [.NET/C# MVP]

André,

The NewRow method is not going to create an instance of the STMTTRNRow
(you might want to reconsider the name there, as it violates naming
guidelines, and is just plain confusing), but rather, it will create an
instance of the DataRow class.

You can't override the NewRow method, so you will have to create an
instance of your row manually, and attach it to the table.

Hope this helps.
 
A

André Fereau

Thanks Nicholas,

How can I create a row manually in a class derived from DataRow? The DataRow
constructor is not available.

André
 
N

Nicholas Paldino [.NET/C# MVP]

André,

You created a class that derives from DataRow, right? You can expose
whatever constructor you want. The constructor for DataRow is internal
protected, meaning that if you derive from the class, you can access the
base constructor.
 
A

André Fereau

Nicholas,

I can access the base constructor DataRow(DataRowBuilder rb) but not
DataRow() and I know no mean to create a DataReowBuilder instance.

public class STMTTRNRow : System.Data.DataRow
{
private STMTTRNDataTable tableSTMTTRN;
internal STMTTRNRow() : base() // wrong prototype error!
{
}
internal STMTTRNRow(DataRowBuilder rb) : base(rb) // OK
{
this.tableSTMTTRN = ((STMTTRNDataTable)(this.Table));
}
.............
 
N

Nicholas Paldino [.NET/C# MVP]

André,

I didn't realize that you couldn't create a DataRowBuilder.

What I would do is create a typed data set, and then look at the
generated code to see how it is creating the typed data rows.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

André Fereau said:
Nicholas,

I can access the base constructor DataRow(DataRowBuilder rb) but not
DataRow() and I know no mean to create a DataReowBuilder instance.

public class STMTTRNRow : System.Data.DataRow
{
private STMTTRNDataTable tableSTMTTRN;
internal STMTTRNRow() : base() // wrong prototype error!
{
}
internal STMTTRNRow(DataRowBuilder rb) : base(rb) // OK
{
this.tableSTMTTRN = ((STMTTRNDataTable)(this.Table));
}
............
 
A

André Fereau

Nicholas,

I've got it! Your idea to build the DataRow manually is the good one.
DataRow is an array of object, it's possible to feed the array. With strong
typed table, we have exception if one object has a wrong type (I want it to
be so).

Regards,

André

public DataRow AddSTMTTRNRow(int BANKACCTFROM, string FITID, System.Byte
TRNTYPE, System.DateTime DTPOSTED, System.DateTime DTUSER, System.DateTime
DTAVAIL, System.Decimal TRNAMT, string CHECKNUM, string SIC, string PAYEEID,
string NAME, string MEMO, System.DateTime DTUPD) {
DataRow rowSTMTTRNRow = this.NewRow();
rowSTMTTRNRow.ItemArray = new object[] {
BANKACCTFROM,
0,
TRNTYPE,
DTPOSTED,
DTUSER,
DTAVAIL,
TRNAMT,
FITID,
CHECKNUM,
"REFNUM",
SIC,
PAYEEID,
NAME,
MEMO
};
this.Rows.Add(rowSTMTTRNRow);
return rowSTMTTRNRow;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top