Accessing data table column name

Posted on 2014-10-08
Last Modified: 2014-11-18

l have a simple loop that loops through all the rows in a data table.  At any point within this loop I need to access the column name?

Can't seem to figure out how this can be done?

Any suggestions?
Question by:andyw27

Expert Comment

ID: 40370041
I need more information about your environment. Is this a VB.NET app? DAO? C? MS Access? Using a particular activex control?
LVL 15

Accepted Solution

Haris Djulic earned 500 total points
ID: 40370083
Not clear about the question, but if you want to get the name of the column you can access it at any time in the loop by using the column.columnname property. Now te way you access it is almost the same in C# and ..


Private Sub PrintColumnNames(dataSet As DataSet)
    Dim table As DataTable
    Dim column As DataColumn 

    ' For each DataTable, print the ColumnName. 
    For Each table in dataSet.Tables
        For Each column in table.Columns
End Sub 

Open in new window

private void PrintColumnNames(DataSet dataSet)
    // For each DataTable, print the ColumnName. 
    foreach(DataTable table in dataSet.Tables)
        foreach(DataColumn column in table.Columns)

Open in new window

LVL 22

Expert Comment

by:Ivo Stoykov
ID: 40370086
Well, it depends how do you approach database.
With DataTable you have ColumnName property for each DataColumn
So using DataColumnCollection you could do something like
foreach(DataColumn column in MyDataTable.Columns)

Open in new window

If you're using (Sql)DataReader just use GetName method, i.e.

var reader = cmd.ExecuteReader();
for(int i=0;i<reader.FieldCount;i++)

Open in new window


Ivo Stoykov

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now