Why can't I enumerate a DataViewRow?

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

This test is generating a compile error:

private void BuildFTPRec(DataRowView a, DataRowView h)
{
foreach (DataRow r in a)
{
foreach (DataColumn dc in r)
{
Console.WriteLine(r.ToString());
}
}
}

The error is:
C:\Data\projects\TwsFtpOutTest\TwsFtpOutTest\Class1.cs(59): foreach
statement cannot operate on variables of type 'System.Data.DataRow' because
'System.Data.DataRow' does not contain a definition for 'GetEnumerator', or
it is inaccessible
 
Bill said:
This test is generating a compile error:

private void BuildFTPRec(DataRowView a, DataRowView h)
{
foreach (DataRow r in a)
{
foreach (DataColumn dc in r)
{
Console.WriteLine(r.ToString());
}
}
}

The error is:
C:\Data\projects\TwsFtpOutTest\TwsFtpOutTest\Class1.cs(59): foreach
statement cannot operate on variables of type 'System.Data.DataRow' because
'System.Data.DataRow' does not contain a definition for 'GetEnumerator', or
it is inaccessible

For exactly the reason it says. A DataViewRow points to a single DataRow,
and a DataRow isn't enumerable either. The DataRow has a Table which has a
Columns collection, though.

Like this:


private void BuildFTPRec(DataView dv)
{
foreach (DataRowView r in dv)
{
foreach (DataColumn dc in r.Row.Table.Columns)
{
Console.Write(r.Row[dc].ToString());
}
}
}

David
 
Back
Top