0

I am trying to access the List-Property of the DataColumnCollection of a DataTable, and it seems like there is none. The access is necessary, for there is a collection of strings ("Format") that I need to process after the extraction from the DataTable.

The documentation of Microsoft says, that it is accessible via the Columns of the DataTable (https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=net-6.0), but it isn't.

foreach (object item in dataTable.Columns.List)  // .List is marked as error
{
    Console.WriteLine("TEST: " + item.Format);   // .Format (is string) is marked as error
}

I know that the List-Property is at least getable and that's all I need. I believe that I'm missing something very obvious here... any Ideas?

Palle Due
  • 5,929
  • 4
  • 17
  • 32
Nepomuk
  • 21
  • 4
  • Object doesn't have a format property. Use var instead of object, then item will have the actual type of what is in dataTable.Columns.List. – Palle Due Dec 21 '21 at 11:02

3 Answers3

1

DataColumnCollection is derived from InternalDataCollectionBase which itself is a collection, so your dataTable.Columns themselves is a list, you don't need to do columns.List

This is how you can access the properties of each column.

DataColumnCollection columns = table.Columns;

// Get the ColumnName and DataType for every column.
foreach(DataColumn dataColumn in columns)
{
    Console.WriteLine(dataColumn.ColumnName);
    Console.WriteLine(dataColumn.DataType);
}
Pankaj
  • 2,618
  • 3
  • 25
  • 47
  • 1
    Probably easier to say "because the class is a microsoft one with a name that ends in Collection, it should have an enumerator.." - because these classes are ooooold and implemen t IEnumerable, not IEnumerable they give their elements out as objects, and the compiler writes a cast when it munges the foreach into the iterating expression.. Which is why `foreach(var x in datatable.Columns)` doesn't work immediately (x becomes object and must be cast) – Caius Jard Dec 22 '21 at 12:43
0

Try to use linq

var dtColumn = from item in dataTable.Columns.Cast<DataColumn>()
select item.ColumnName;
Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
0

It looks like I found the Answer:

foreach (TableColumn item in dataTable.Columns) 
{
    Console.WriteLine("TEST: " + item.Format);  
}

Seems like the type TableColumn inherits from DataColumn and the property Format can be directly accessed.

Nepomuk
  • 21
  • 4
  • This will work if the datatable.Columns collection does contain a TableColumn instance (whatever that is; neither the DocumentFormat.OpenXml.Spreadsheet.TableColumn, nor System.Windows.Documents.TableColumn seem to inherit from DataColumn, nor do they contain a Format property).. – Caius Jard Dec 22 '21 at 12:41
  • Incidentally, without knowing that you've filled your datatable with some unknown type TableColumn which has a Format, we wouldn't have been able to arrive at a helpful answer.. If you do stuff that's non-standard, let us know so we can target the advice – Caius Jard Dec 22 '21 at 12:46
  • 1
    Will do @CaiusJard. I wasn't aware that it would be non-standard. Thx for contributing. – Nepomuk Dec 23 '21 at 09:47