ArrayList to DataTable

Posted: June 2, 2015 in Arraylist, C#, Collections, DataTable

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s