Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:56:05+00:00 2026-05-14T05:56:05+00:00

In a DataTable object, is there added overhead to looking up a column value

  • 0

In a DataTable object, is there added overhead to looking up a column value by name thisRow("ColumnA") rather than by the column index thisRow(0)? In which scenarios might this be an issue.

I work on a team that has lots of experience writing VB6 code and I noticed that didn’t do column lookups by name for DataTable objects or data grids. Even in .NET code, we use a set of integer constants to reference column names in these types of objects. I asked our team lead why this was so, and he mentioned that in VB6, there was a lot of overhead in looking up data by column name rather than by index. Is this still true for .NET?


Example code (in VB.NET, but same applies to C#):

Public Sub TestADOData()
Dim dt As New DataTable

'Set up the columns in the DataTable    '
dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Description", GetType(String)))

'Add some data to the data table    '
dt.Rows.Add(1, "Fred", "Pitcher")
dt.Rows.Add(3, "Hank", "Center Field")

'Method 1: By Column Name   '
For Each r As DataRow In dt.Rows
  Console.WriteLine( _
   "{0,-2} {1,-10} {2,-30}", r("ID"), r("Name"), r("Description"))
Next

Console.WriteLine()

'Method 2: By Column Name   '
For Each r As DataRow In dt.Rows
  Console.WriteLine("{0,-2} {1,-10} {2,-30}", r(0), r(1), r(2))
Next

End Sub

Is there an case where method 2 provides a performance advantage over method 1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:56:05+00:00Added an answer on May 14, 2026 at 5:56 am

    Yes, there should be a slight overhead connected to looking up columns by name instead of by index. I wouldn’t worry about it, unless you keep looking up that same column in a loop, like in your code example. Because then the slight overhead might accumulate to a measurable overhead, depending on the table’s number of rows.

    The fastest way to access a particular column’s value of some row is to lookup using the DataColumn object itself. For example:

    Dim dt As DataTable = ...
    
    Dim idColumn As DataColumn = dt.Columns("ID")
    Dim nameColumn As DataColumn = dt.Columns("Name")
    Dim descriptionColumn As DataColumn = dt.Columns("Description")
    
    For Each r As DataRow In dt.Rows
    
        ' NB: lookup through a DataColumn object, not through a name, nor an index: '
        Dim id = r(idColumn)
        Dim name = r(nameColumn)
        Dim description = r(descriptionColumn)
    
        ...
    Next
    

    One last piece of advice: I would strongly advise you against using numerical indices! It makes your code more fragile, and also more difficult to understand and maintain: As soon as the logical order of a column changes, you need to adapt your code accordingly, possibly in several places (and you might easily oversee one of them, leading to bugs). If you instead use column names or DataColumn objects themselves for the lookup, you can change your columns’ order without having to change the remaining code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are several queries to be performed which return the DataTable object. In order
I have a DataTable object. Every column is of type string. Using LINQ, how
There are three column in the datatable A,B and C . Now each column
In a .NET Datatable, the columns are Object types, which can include a datatable
I have DataTable object with next data: Id Value 1 val1 3 val2 2
When accessing an object in a DataTable retrieved from a database, are there any
As I iterate through a DataTable object, I need to check each of its
How do I add a new DataColumn to a DataTable object that already contains
How do I convert a datatable into a POCO object in Asp.Net MVC?
Using Script Task, I had stored data ie a Dataset or Datatable object to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.