Getting value from field in DataTable when column name has spaces

27,104

Have you checked what the column thinks it is called? It might have underscores, for example. Loop over the columns and find out (sorry, examples in C#):

foreach(DataColumn col in table.Columns) {
    Debug.WriteLine(col.ColumnName);
}

Actually, it is faster to use the column if you are doing it in a loop, so I might use something like:

DataColumn col = table.Columns["whatever"];
foreach(DataRow row in table.Rows) {
    Console.WriteLine(row[col]);
}
Share:
27,104
Marcelo
Author by

Marcelo

Updated on March 09, 2020

Comments

  • Marcelo
    Marcelo about 4 years

    I have tried:

    ObjDTOleDBNFeIntegra.Rows(I)("[Cnpj Cpf]").ToString() //with brackets  
    ObjDTOleDBNFeIntegra.Rows(I)("'Cnpj Cpf'").ToString() //with apostrophe  
    ObjDTOleDBNFeIntegra.Rows(I)("Cnpj Cpf").ToString() //without anything  
    

    I'm using VB.NET, but comments with apostrophes in here don't seem to be identified.

    And I get the exceptions for each case:
    Column '[Cnpj Cpf]' does not belong to table Table. (fail) Column 'Cnpj Cpf' does not belong to table Table.    (fail) Column ''Cnpj Cpf'' does not belong to table Table. (fail)

    What should I do in order to ger a value from a field in a dataTable when the column name has spaces ?