-
Sep 19th, 2017, 07:03 PM
#1
Thread Starter
Lively Member
Help with array / losing data in elements!
Could someone please tell me what's going wrong here, my values are not being stored in the array, as i'd hoped... What's happening is the stored values in the elements are being wiped out to "Nothing", with only the last cycled element having stored data.
Thanks for any advice!
Code:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i, j As Integer
Dim r As Integer = DataGridView1.SelectedCells.Item(i).RowIndex
Dim DataArray() As String
For Each cell As DataGridViewCell In DataGridView1.SelectedCells
SelectedItemsListBox.Items.Add(DataGridView1.SelectedCells.Item(i).RowIndex & " - " & DataGridView1.Item(1, r).Value)
For i = 0 To SelectedItemsListBox.Items.Count - 1
For j = SelectedItemsListBox.Items.Count - 1 To (i + 1) Step -1
If SelectedItemsListBox.Items(i) = SelectedItemsListBox.Items(j) Then
SelectedItemsListBox.Items.Remove(SelectedItemsListBox.Items(j))
SelectedItemsListBox.Sorted = True
End If
Next
Next
ReDim Preserve DataArray(j)
DataArray(j) = DataGridView1.Item(1, r).Value.ToString
Next
Array.Sort(DataArray)
End Sub
-
Sep 19th, 2017, 07:35 PM
#2
Re: Help with array / losing data in elements!
You've posted this in the VB.NET CodeBank forum, which is for sharing working VB.NET code snippets with others. I've asked the mods to move this thread to the VB.NET forum, which is for asking questions relating to VB.NET.
-
Sep 19th, 2017, 07:43 PM
#3
Re: Help with array / losing data in elements!
Maybe it would help if you were to provide an explanation of what you're trying to achieve too, because your code makes no sense to me. I have no doubt that there's a far better way to achieve your aim.
As for your array, what's the point of it? You create it inside the method, populate it, sort it and then simply discard it. Is the intention to use it outside of that method? If so then you'd need to assign it to a member variable. Have you not learned about variable scope yet?
Also, I'd suggest using a List(Of String) in that method. If you find yourself using ReDim then you should almost certainly be using a collection rather than an array. If you need an array specifically then you can call ToArray on the collection as required.
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
|