DataGrid - Binding to an ArrayList of Structures

G

Guest

I am trying to bind a DataGrid Control (named DataGrid1) to an array list
containing elements of a sturcture type (named TestDataGrid). From what I've
read, it appears possible but the following code doesn't show the data nor
the column headings in the DataGrid control:

Public Structure TestDataGrid
Public Col1 As String
Public Col2 As String
End Structure

public ar As ArrayList = New ArrayList

Sub DisplayDatainGrid(dGrid as DataGrid)
Dim j As TestDataGrid
j.Col1 = "AAA" : j.Col2 = "BBB"
ar.Add(j)
j.Col1 = "111" : j.Col2 = "222"
ar.Add(j)

Dim ts1 As New DataGridTableStyle
ts1.MappingName = "TestDataGrid"

Dim c1 As New DataGridTextBoxColumn
c1.MappingName = "Col1"
c1.HeaderText = "Column1"
c1.Width = 100
ts1.GridColumnStyles.Add(c1)

Dim c2 As New DataGridTextBoxColumn
c2.MappingName = "Col2"
c2.HeaderText = "Column2"
c2.Width = 100
ts1.GridColumnStyles.Add(c2)

dGrid.TableStyles.Add(ts1)

dgrid.DataSource = ar
end sub
 
K

Ken Tucker [MVP]

Hi,

You need to bind to an arraylist of a class. The grid will show any
properties contained in the class. A structure wont work.

Public Class TestDataGrid

private mstrcol1 as string
private mstrcol2 as string

Public Property Col1() As String

Get

Return mstrCol1

End Get

Set(ByVal Value As String)

mstrCol1 = Value

End Set

End Property

Public Property Col2() As String

Get

Return mstrCol2

End Get

Set(ByVal Value As String)

mstrCol2 = Value

End Set

End Property

End class


Ken
-----------------
I am trying to bind a DataGrid Control (named DataGrid1) to an array list
containing elements of a sturcture type (named TestDataGrid). From what
I've
read, it appears possible but the following code doesn't show the data nor
the column headings in the DataGrid control:

Public Structure TestDataGrid
Public Col1 As String
Public Col2 As String
End Structure

public ar As ArrayList = New ArrayList

Sub DisplayDatainGrid(dGrid as DataGrid)
Dim j As TestDataGrid
j.Col1 = "AAA" : j.Col2 = "BBB"
ar.Add(j)
j.Col1 = "111" : j.Col2 = "222"
ar.Add(j)

Dim ts1 As New DataGridTableStyle
ts1.MappingName = "TestDataGrid"

Dim c1 As New DataGridTextBoxColumn
c1.MappingName = "Col1"
c1.HeaderText = "Column1"
c1.Width = 100
ts1.GridColumnStyles.Add(c1)

Dim c2 As New DataGridTextBoxColumn
c2.MappingName = "Col2"
c2.HeaderText = "Column2"
c2.Width = 100
ts1.GridColumnStyles.Add(c2)

dGrid.TableStyles.Add(ts1)

dgrid.DataSource = ar
end sub
 
G

Guest

Thanks for help. I got it working by using classes instead of structures.
However, I can't get it to sort. If I read the AllowSorting Property for
GridTableStyles, the sorting by column only works if the data is contained in
a Dataset...do you know if this is correct?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top