Alex Rivera | Logout

In vb.net, how to get the column names from a datatable

Asked 2011-07-22T14:58:49.767
17

So i create a datatable from a table i can't directly view(i.e. using sql server management). I want to find the column names that are in the datatable, what would be the correct way do this?

Edit
Report

1 Answer

5

You can loop through the columns collection of the datatable.

VB

Dim dt As New DataTable()
For Each column As DataColumn In dt.Columns
    Console.WriteLine(column.ColumnName)
Next

C#

DataTable dt = new DataTable();
foreach (DataColumn column in dt.Columns)
{
Console.WriteLine(column.ColumnName);
}

Hope this helps!

answered 2011-07-22T15:03:35.353

Your Answer