We construct a DataTable using DataColumns. The example adds some rows to the DataTable based on the types and count of the columns specified by the DataColumn references.
using System;
using System.Data;
class Program
{
static void Main()
{
//
// Construct the DataTable.
//
DataTable table = GetTable();
//
// Loop over the column headers in the DataTable.
//
foreach (DataColumn column in table.Columns)
{
Console.WriteLine(
"{0} = {1}", column, column.DataType);
}
}
/// <summary>
/// Generates a DataTable with four columns.
/// </summary>
static DataTable GetTable()
{
//
// Here we create a DataTable and add columns to it.
//
DataTable table = new DataTable();
table.Columns.Add(
"Dosage", typeof(int));
table.Columns.Add(
"Medication", typeof(string));
table.Columns.Add(
"Patient", typeof(string));
//
// Add another column to the data table in a different way.
//
DataColumn column = new DataColumn(
"Appointment", typeof(DateTime));
table.Columns.Add(column);
//
// Here we add some DataRows.
// Note that the row parameters must match the order and types of the columns.
//
table.Rows.Add(21,
"Combivent",
"Janet", DateTime.Now);
table.Rows.Add(100,
"Dilantin",
"Melanie", DateTime.Now);
return table;
}
}
Dosage = System.Int32
Medication = System.String
Patient = System.String
Appointment = System.DateTime