Connecting Tech Pros Worldwide Forums | Help | Site Map

Updating a DataGrid using an ArrayList

Jeffrey Spoon
Guest
 
Posts: n/a
#1: Nov 21 '05


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.





--
Jeffrey Spoon


Jeffrey Spoon
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Updating a DataGrid using an ArrayList


In message <n4KdfKC1tRZCFwNg@nowhere.nnn>, Jeffrey Spoon
<JeffreySpoon@hotmail.com> writes
[color=blue]
>
>Public Class Person
>
> Public Reading As Integer
> Public ReadingValue As Integer
>
>End Class[/color]

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



--
Jeffrey Spoon

Cor Ligthert
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Updating a DataGrid using an ArrayList


Jseffrey,

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

Cor


Jeffrey Spoon
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Updating a DataGrid using an ArrayList


In message <OV9vwMPRFHA.3048@TK2MSFTNGP10.phx.gbl>, Cor Ligthert
<notmyfirstname@planet.nl> writes[color=blue]
>Jseffrey,
>
>Do you have any reason that it should be an arraylist and not a perfectly
>fitting datatable?
>
>Cor
>
>[/color]

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




--
Jeffrey Spoon

Cor Ligthert
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Updating a DataGrid using an ArrayList


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


Jeffrey Spoon
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Updating a DataGrid using an ArrayList


In message <eycC2gPRFHA.2252@TK2MSFTNGP15.phx.gbl>, Cor Ligthert
<notmyfirstname@planet.nl> writes[color=blue]
>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
>
>[/color]

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.




--
Jeffrey Spoon

Kerry Moorman
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Updating a DataGrid using an ArrayList


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


"Jeffrey Spoon" wrote:
[color=blue]
>
>
> 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.
>
>
>
>
>
> --
> Jeffrey Spoon
>
>[/color]
Jeffrey Spoon
Guest
 
Posts: n/a
#8: Nov 21 '05

re: Updating a DataGrid using an ArrayList


In message <7CF429CF-650E-408E-A847-C12C3811CB85@microsoft.com>, Kerry
Moorman <KerryMoorman@discussions.microsoft.com> writes[color=blue]
>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
>[/color]


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.myArra y, "")
Chart1.ChartData = ReadingDataGrid

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

'Add titles to the axes.
With Me.Chart1.Plot
.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdX).AxisTit le.Text =
"Year"
.Axis(MSChart20Lib.VtChAxisId.VtChAxisIdY).AxisTit le.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




--
Jeffrey Spoon

Jeffrey Spoon
Guest
 
Posts: n/a
#9: Nov 21 '05

re: Updating a DataGrid using an ArrayList


In message <eycC2gPRFHA.2252@TK2MSFTNGP15.phx.gbl>, Cor Ligthert
<notmyfirstname@planet.nl> writes[color=blue]
>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
>
>[/color]

Thanks Cor. It certainly looks easier.



--
Jeffrey Spoon

Closed Thread