Archive for the ‘DataTable’ Category

Below is the simple script to a collection (arraylist – > DataTable) :

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;

namespace Datatable
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(“The”);
al.Add(“quick”);
al.Add(“brown”);
al.Add(“fox”);

// Create new DataTable.
DataTable table = new DataTable(“table”);

// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;

// Create column.
column = new DataColumn();
column.DataType = Type.GetType(“System.String”);
column.ColumnName = “item”;
table.Columns.Add(column);

// Add items from ArrayList to DataTable.
foreach (String item in al)
{
row = table.NewRow();
row[“item”] = item;
table.Rows.Add(row);
}
}
}
}

 

Advertisements