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?
 
I'll preface this by saying you should be using a List(of T) rather than an ArrayList to prevent typecasting issues.

VB.NET:
		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)
 
a simplification:
VB.NET:
ds.Tables(0).Rows.Add(line.Split("|"c))
 

Latest posts

Back
Top