Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are several ways to print or display the contents of a DataTable:

  1. Loop through each row and column and print the values:
foreach (DataRow row in dataTable.Rows)
{
    foreach (DataColumn col in dataTable.Columns)
    {
        Console.Write(row[col] + "\t");
    }
    Console.WriteLine();
}
  1. Use the DataTable's built-in WriteXml method to write the contents to an XML file:
dataTable.WriteXml("data.xml");
  1. Bind the DataTable to a data control (such as a DataGridView or GridView) and display the contents on a form:
dataGridView.DataSource = dataTable;
  1. Use LINQ to query the DataTable and display the results:
var results = from DataRow row in dataTable.Rows
              select row["ColumnName"];
foreach (var row in results)
{
    Console.WriteLine(row);
}