ArrayList to DataGrid

jpnoob

Member
Joined
Jun 4, 2009
Messages
8
Programming Experience
1-3
I have an ArrayList that are seperated by the pipe:

colA | colB | colC | colD

how can I bind that array into separate columns in a datagrid each with their own column header?
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
I'll preface this by saying you should be using a List(of T) rather than an ArrayList to prevent typecasting issues.

Code:
		Dim theList As New ArrayList
		theList.Add("a|b|c|d")
		theList.Add("e|f|g|h")
		theList.Add("i|j|k|l")

		Dim ds As New DataSet()
		ds.Tables.Add(New DataTable)
		ds.Tables(0).Columns.Add("col1", GetType(System.String))
		ds.Tables(0).Columns.Add("col2", GetType(System.String))
		ds.Tables(0).Columns.Add("col3", GetType(System.String))
		ds.Tables(0).Columns.Add("col4", GetType(System.String))

		For Each line As String In theList
			Dim theRow As DataRow = ds.Tables(0).NewRow
			For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
				theRow(i) = line.Split("|"c)(i)
			Next
			ds.Tables(0).Rows.Add(theRow)
		Next

		Me.DataGridView1.DataSource = ds.Tables(0)
 
Top Bottom