Ask Your Question
4

How can the contents of a DataTable be printed?

asked 2023-04-30 00:04:55 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-04-30 00:27:02 +0000

ladyg gravatar image

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);
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-30 00:04:55 +0000

Seen: 10 times

Last updated: Apr 30 '23