How to access column in a datatable

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

rmmarshAsked:
Who is Participating?
 
regevhaConnect With a Mentor Commented:
Detailed information with code samples regarding accessing data using DataTable can be found at "CodeProject" site, "Practical guide to .NET DataTables ..."  - http://www.codeproject.com/KB/grid/practicalguidedatagrids2.aspx

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
  TableData.AppendFormat(“Row”);
  foreach (DataColumn dc in dt.Columns)
  TableData.AppendFormat(“\t{0}”, dc.ColumnName);
  TableData.AppendFormat(“\n”);
  
  // retrieve rows
  int j = -1;
  foreach (DataRow dr in dt.Rows)
  {
    TableData.AppendFormat(“[{0}]”,++j);
    foreach (DataColumn dc in dt.Columns)
    {
     TableData.AppendFormat(“\t{0}”, dr[dc] );
    }
   TableData.AppendFormat(“\n”);
  }

  return TableData.ToString();

}

Open in new window


If you would like further assistance, please let us know.
0
 
rmmarshAuthor Commented:
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];
0
All Courses

From novice to tech pro — start learning today.