How to access column in a datatable

Posted on 2011-10-14
Last Modified: 2013-12-16
Quite a while ago, while learning C#, I wrote this snippet of code, which has been working so far.  I need to get to column[6] for each row in the table and examine the contents.

How do I do this?
DataTable dt = new DataTable();
            FbDataAdapter da = new FbDataAdapter("SELECT * from tUploadInfo", mainForm.bookConn);
            da.Fill(dt);  //  fill datatable

Open in new window

Question by:rmmarsh
    LVL 6

    Accepted Solution

    Detailed information with code samples regarding accessing data using DataTable can be found at "CodeProject" site, "Practical guide to .NET DataTables ..."  -

    Regarding your specific question, the solution is described in the following code:
    private string GetTableData(DataTable dt)
      StringBuilder TableData = new StringBuilder();
      // retrieve header row column labels
      foreach (DataColumn dc in dt.Columns)
      TableData.AppendFormat(“\t{0}”, dc.ColumnName);
      // retrieve rows
      int j = -1;
      foreach (DataRow dr in dt.Rows)
        foreach (DataColumn dc in dt.Columns)
         TableData.AppendFormat(“\t{0}”, dr[dc] );
      return TableData.ToString();

    Open in new window

    If you would like further assistance, please let us know.

    Author Closing Comment

    I will give you the points, but this one line of code does exactly what I was looking for.

                    object ei = dt.Rows[0][6];

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Normally a window is moved by clicking on the caption bar and dragging. You may want your user to be able to move borderless forms or move a form by clicking anywhere in the form without the limitation to the caption bar. There are many ways to do i…
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    16 Experts available now in Live!

    Get 1:1 Help Now