473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Updating a DataGrid using an ArrayList



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

Nov 21 '05 #1
8 1976
In message <n4**************@nowhere.nnn>, Jeffrey Spoon
<Je**********@hotmail.com> writes

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?

--
Jeffrey Spoon

Nov 21 '05 #2
Jseffrey,

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

Cor
Nov 21 '05 #3
In message <OV**************@TK2MSFTNGP10.phx.gbl>, Cor Ligthert
<no************@planet.nl> writes
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


--
Jeffrey Spoon

Nov 21 '05 #4
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
Nov 21 '05 #5
In message <ey**************@TK2MSFTNGP15.phx.gbl>, Cor Ligthert
<no************@planet.nl> writes
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.


--
Jeffrey Spoon

Nov 21 '05 #6
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:


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

Nov 21 '05 #7
In message <7C**********************************@microsoft.co m>, Kerry
Moorman <Ke**********@discussions.microsoft.com> writes
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.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

Nov 21 '05 #8
In message <ey**************@TK2MSFTNGP15.phx.gbl>, Cor Ligthert
<no************@planet.nl> writes
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.

--
Jeffrey Spoon

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen | last post by:
I've got a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is...
4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
0
by: Empire City | last post by:
I have an ASP.NET form with a DataGrid and Button. I want to put a RadioButtonList in a DataGrid cell. I bind it to an ArrayList which has a ListItem in the cell. The display part works fine. I...
1
by: Empire City | last post by:
I have an ASP.NET form with a DataGrid and Button. I want to put a RadioButtonList in a DataGrid cell. I bind it to an ArrayList which has a ListItem in the cell. The display part works fine. I...
1
by: Patrick Delifer | last post by:
HI, I am trying to update a DataGrid where I want to rebind just the row that was updated. Here's the situation: 1) Updating is not a problem, it works fine. 2) I am not using ItemCommand (edit,...
5
by: Dennis | last post by:
I have a class that inherits from DataGrid. I can set the rowheights in a DataGrid by tappig into the "get_Datagridrows" method. However, this does not work for classes that inherit from...
16
by: stojilcoviz | last post by:
I've a datagrid whose datasource is an arraylist object. The arraylist holds many instances of a specific class. I've two questions about this: 1 - Is there a way by which I can obtain a...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.