Convert ArrayList to DataTable in C#


How to Convert ArrayList to DataTable in C#. Below is the code snippet to demonstrate how to transfer the data from ArrayList to DataTable in C#. For Example I have one ArrayList and added the Items to arrayList, If I want to send the data from ArrayList to DataTable, below is the solution for that.


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

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{

ArrayList MyArrayList = new ArrayList();

MyArrayList.Add("The");

MyArrayList.Add("quick");
MyArrayList.Add("brown");
MyArrayList.Add("fox");

DataTable table = new DataTable("table");
DataColumn column;
DataRow row;

column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "SyntaxHelp";
table.Columns.Add(column);

foreach (String item in MyArrayList)
{
row = table.NewRow();
row["SyntaxHelp"] = item;
table.Rows.Add(row);

}

}
}
}


Comments

Author: Vinod12 Jul 2012 Member Level: Gold   Points : 0

hi Naveen Reddy,
it is possible to do reverse?
from data table to array list

thank you.
:)



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: