Thursday, April 2, 2009

ArrayList to DataTable

From last couple of days, i found few programmer in asp.net forum wanted to convert ArrayList to DataTable. So i thought to put it here for reference.

Below is the simple example of converting double dimensional array list into data table.

Dim arrList As ArrayList = New ArrayList()
Dim dt As New DataTable()
Dim intCnt As Integer = 0
dt.Columns.Add(New DataColumn("Name", System.Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("Age", System.Type.GetType("System.String")))
arrList.Add(New String() {"Richards", "25"})
arrList.Add(New String() {"Marie", "30"})
arrList.Add(New String() {"Sandy", "35"})
arrList.Add(New String() {"Michael", "40"})
For Each item As Object In arrList
Dim dr As DataRow
Dim arrItem As String() = DirectCast(item, String())
dr = dt.NewRow()
dr("Name") = arrItem(0)
dr("Age") = arrItem(1)
dt.Rows.Add(dr)
Next

Happy Coding :)

No comments:

Site Meter