Updating a DataGrid using an ArrayList

J

Jeffrey Spoon

Hello, according to the docs you can update a DataGrid with an
ArrayList. I have created an ArrayList and I add these objects:

Dim patient1 As New Person
patient1.Reading = 1
patient1.ReadingValue = 50
myArray.add(person1)
Dim patient2 As New Person
patient2.Reading = 2
patient1.ReadingValue = 60
myArray.add(person1)

Then I bind it to a DataGrid:

DataGrid1.DataSource = myArray
DataGrid1.SetDataBinding(myArray, "")


Public Class Person

Public Reading As Integer
Public ReadingValue As Integer

End Class

I have set the DataGridTableStyle with the MappingName myArray.
It does update the DataGrid with two entries, but they don't contain
anything (AFAIK) How do I get the Reading and ReadingValue fields into
the Datagrid via the ArrayList?

I'm hoping to eventually update an MSChart using the datagrid but I'm
not sure if the Datagrid actually contains any data.
 
J

Jeffrey Spoon

Jeffrey Spoon said:
Public Class Person

Public Reading As Integer
Public ReadingValue As Integer

End Class

Oops, Integer doesn't have public properties that's why. This brings me
to the question, how do I display them?
 
C

Cor Ligthert

Jseffrey,

Do you have any reason that it should be an arraylist and not a perfectly
fitting datatable?

Cor
 
J

Jeffrey Spoon

Cor Ligthert said:
Jseffrey,

Do you have any reason that it should be an arraylist and not a perfectly
fitting datatable?

Cor

Not really (other than not knowing what I'm doing) It's just that I add
the integers I need to an ArrayList. The main purpose is to dynamically
update an MSChart so I'm not too fussy.

Thanks
 
C

Cor Ligthert

Jeffrey,

Is this not more easy?
\\\
Dim dt As New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("id")
dt.LoadDataRow(New Object() {"Patient1", "1"}, True)
dt.LoadDataRow(New Object() {"Patient2", "2"}, True)
DataGrid1.DataSource = dt
///
I hope this helps,

Cor
 
J

Jeffrey Spoon

Cor Ligthert said:
Jeffrey,

Is this not more easy?
\\\
Dim dt As New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("id")
dt.LoadDataRow(New Object() {"Patient1", "1"}, True)
dt.LoadDataRow(New Object() {"Patient2", "2"}, True)
DataGrid1.DataSource = dt
///
I hope this helps,

Cor

It looks it, thanks. I'm not very familiar with all the components as
I'm a newbie. You're the second person to suggest DataTables so I'll
have a look at them.

Cheers.
 
G

Guest

Jeffrey,

Databinding won't work with public fields.

Instead of using public fields in the Person class, you need to use private
fields with public properties.

Kerry Moorman
 
J

Jeffrey Spoon

Kerry said:
Jeffrey,

Databinding won't work with public fields.

Instead of using public fields in the Person class, you need to use private
fields with public properties.

Kerry Moorman


Thanks. I did that and the DataGrid works now. The trouble is the Chart
won't take the DataGrid. I don't know why. The line
Chart1.ChartData = ReadingDataGrid gives a Bad Function argument. I take
it there is more to do with the DataGrid than just assigning it to
ChartData?


Public Class Form1
Inherits System.Windows.Forms.Form



Private number = 10
'Public myArray = New ArrayList
Public myCollection = New CollectionList

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Chart()
End Sub

Sub Chart()


Dim patient1 As New Patient
patient1.ReadingProperty = 1
patient1.ReadingValueProperty = 50
myCollection.add(patient1)
Dim patient2 As New Patient
patient2.ReadingProperty = 2
patient2.ReadingValueProperty = 60
myCollection.add(patient2)


ReadingDataGrid.SetDataBinding(myCollection.myArray, "")
Chart1.ChartData = ReadingDataGrid

'Add a title and legend.
With Me.Chart1
.Title.Text = "Sales"
.Legend.Location.LocationType = _
MSChart20Lib.VtChLocationType.VtChLocationTypeBottom
.Legend.Location.Visible = True
End With

'Add titles to the axes.
With Me.Chart1.Plot
.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).AxisTitle.Text =
"Year"
.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).AxisTitle.Text =
"Millions of $"
End With

'Set custom colors for the bars.
With Me.Chart1.Plot
'Yellow for Company A
' -1 selects all the datapoints.
.SeriesCollection(1).DataPoints(-1).Brush.FillColor.Set(250,
250, 0)
'Purple for Company B
'.SeriesCollection(2).DataPoints(-1).Brush.FillColor.Set(200,
50, 200)
End With

End Sub

End Class

Public Class CollectionList

Public myArray As ArrayList


Public Sub New()
myArray = New ArrayList
End Sub
Public Sub Add(ByRef T As Patient)
myArray.Add(T)
End Sub
Public Sub Remove(ByRef T As Patient)
myArray.Remove(T)
End Sub
Public ReadOnly Property Count() As Integer
Get
Return myArray.Count
End Get
End Property
Public Function GetTrack(ByVal index As Integer) As Patient
Return myArray.Item(index)
End Function

End Class

Public Class Patient

Private Reading As Integer
Private ReadingValue As Integer

Public Property ReadingProperty() As Integer
Get
Return Reading
End Get
Set(ByVal Value As Integer)
Reading = Value
End Set
End Property
Public Property ReadingValueProperty() As Integer
Get
Return ReadingValue
End Get
Set(ByVal Value As Integer)
ReadingValue = Value
End Set
End Property
End Class


End Class
 
J

Jeffrey Spoon

Cor Ligthert said:
Jeffrey,

Is this not more easy?
\\\
Dim dt As New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("id")
dt.LoadDataRow(New Object() {"Patient1", "1"}, True)
dt.LoadDataRow(New Object() {"Patient2", "2"}, True)
DataGrid1.DataSource = dt
///
I hope this helps,

Cor

Thanks Cor. It certainly looks easier.
 

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