|
-
Aug 8th, 2012, 02:48 PM
#1
Thread Starter
Lively Member
ArrayList view in DataGridView
Hi,
I have created an ArrayList that I am filling with names of cities that I retrieve from an Acces database. No problem there since I have inserted MsgBoxes to check the progress and I can see each name for each city being stored in the array as the procedure is running.
However, in displaying the ArrayList later on in a DataGridView, instead of getting the names of the cities, I get the number of characters for each city name (column header in DataGridview is also called ‘length’). So e.g. ‘London’ becomes ‘6’.
Anyone who knows where I go wrong or how to fix this ? I looked into the properties for the DataGridView, but there seems to be nothing there that can offer any help.
Thanks !
-
Aug 8th, 2012, 03:15 PM
#2
Re: ArrayList view in DataGridView
In the example below you'll see that the first example I give is of an ArrayList containing Person objects. This binds to the DataGridView with no problems.
In the second example I'm using an ArrayList with Strings. If I were to try and bind StringArrayList to the DataGridView you'd get a column named Length with each value showing how long the string is. This is because String is a Reference Type rather than a Value Type. The DataGridView is looking for properties to bind to and since String is a Reference Type the only thing it can see is Length.
The easiest way to get around this is to create a wrapper class around the String with a Value property so the DataGridView can see that.
Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim PersonArrayList As New ArrayList
With PersonArrayList
.Add(New Person With {.FirstName = "Jim", .LastName = "Jones"})
.Add(New Person With {.FirstName = "Peggy", .LastName = "Sue"})
.Add(New Person With {.FirstName = "John", .LastName = "Smith"})
End With
DisplayDataGridView.DataSource = PersonArrayList
Dim StringArrayList As New ArrayList
With StringArrayList
.Add("Test 1")
.Add("Test 101")
.Add("Test 10101")
End With
Dim StringValueArray = Array.ConvertAll(StringArrayList.ToArray(), AddressOf GetStringValue)
StringDataGridView.DataSource = StringValueArray
End Sub
Private Function GetStringValue(s As String) As StringValue
Return New StringValue(s)
End Function
End Class
Public Class Person
Public Property FirstName As String
Public Property LastName As String
Public ReadOnly Property FullName As String
Get
Return String.Join(", ", LastName, FirstName)
End Get
End Property
End Class
Public Class StringValue
Public Property Value As String
Public Sub New(s As String)
_Value = s
End Sub
End Class
All of that being said if you have a version of .NET that supports List(Of T) you should use that rather than an ArrayList.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Aug 8th, 2012, 03:16 PM
#3
Re: ArrayList view in DataGridView
Er ... ArrayList? Array, yes. Generic List, yes. ArrayList, nah!
-
Aug 9th, 2012, 03:27 AM
#4
Thread Starter
Lively Member
Re: ArrayList view in DataGridView
Hello MattP?
Thanks for helping me out.
I am working with VS 2010, so List(OfT) is available to me but I have no experience with it.
I tried to create the wrapper (several alternatives) as indicated by you, but unfortunately without success. The only thing I keep getting now is an empty DataGridView.
Below is the code.
Can you point me out ?
P.S. this code will give an error message because database NULL values are not handled.
Thanks,
Eric
Dim keywords As New ArrayList
Dim i As Short = 0
Dim j As Short = 11
Do Until i = count - 1
'MsgBox("1. show i = " & i)
'MsgBox("1. Show j = " & j)
Do Until j = 21
keywords.Add(ds.Tables("Bib").Rows(i).Item(j))
'MsgBox("2. show i = " & i)
'MsgBox("2. Show j = " & j & " = " & (keywords(0)))
j = j + 1
'MsgBox("3. show i = " & i)
'MsgBox("3. Show j = " & j)
Loop
j = 11
i = i + 1
'MsgBox("4. show i = " & i)
'MsgBox("4. Show j = " & j)
Loop
'MsgBox("Loops are both successfull")
DataGridView1.DataSource = keywords
-
Aug 9th, 2012, 08:52 AM
#5
Re: ArrayList view in DataGridView
The only thing you need to change from my example is
Code:
Dim StringArrayList As New ArrayList
should be
Code:
Dim StringList As New List(Of String)
The other option is to create a List(Of StringValue) and bind to that.
Code:
Dim StringValueList As New List(Of StringValue)
With StringValueList
.Add(New StringValue("Test 1"))
.Add(New StringValue("Test 101"))
.Add(New StringValue("Test 10101"))
End With
StringDataGridView.DataSource = StringValueList
The issue of loading NULL values from the database is a separate question and I'll ask you to create a new thread for it so in the future users searching the forum can find it.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|