2

I want to assign 2D array to DataGridView.

There are 2 buttons, the first is an array button which will add my inputs to the array every time I press it.

The second button is a submit button that will assign all of the values of the array to the DataGridView.

But I just can't work it out, every time I press the array button, the value is replaced by the new value I inputted.

Here's the code:

Public Class Form2
Dim array(1, 4) As String

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    For i = 0 To array.GetUpperBound(0)
        DataGridView1.Rows.Add(array(i, 0), array(i, 1), array(i, 2), array(i, 3))
    Next
End Sub

Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
    ReDim array(1, 2)

    Dim id, name As String
    id = txtID.Text
    name = txtName.Text

    For i = 0 To array.GetUpperBound(0)
        array(i, 0) = id
        array(i, 1) = name
    Next
End Sub
End Class
4
  • I think this is part of your problem ReDim array(1, 2) all that is doing is just changing it from a 1 by 4 dimension to a 1 by 2 dimension. The next problem is that you are just overwriting with the for loop not adding a new dimension to the array. Here is what you need to do. change ReDim array(1, 2) to ReDim array(array.GetUpperBound(0) + 2, 4) take out the for loop and just put ` array(array.GetUpperBound(0) , 0) = id` and array(array.GetUpperBound(0) , 1) = name here is the redim reference Commented Oct 23, 2015 at 15:18
  • Forgot the Preserve on the redim so it should be ReDim Preserve array(array.GetUpperBound(0) + 2, 4) not ReDim array(array.GetUpperBound(0) + 2, 4) Commented Oct 23, 2015 at 15:29
  • @jagler I tried it, but when I clicked the array button, there's an error in the ReDim Preserve array(array.GetUpperBound(0) + 2, 4), it said "An unhandled exception of type 'System.ArrayTypeMismatchException' occurred in Microsoft.VisualBasic.dll. Additional information: 'ReDim' can only change the rightmost dimension.". Should I change the 2D array to jagged array so I can use the ReDim Preserve? Commented Oct 24, 2015 at 13:48
  • personally I would of just used a list of a custom struct. But a jagged array may work. I don't usually use arrays. Plus a list can be converted easily to an array to go into a datagridview. Commented Oct 24, 2015 at 15:21

1 Answer 1

2

This code works for me!

Public Class Form2
Dim arrayCopy(10, 1) As String
Dim b As Integer

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    DataGridView1.Rows.Clear()
    For i = 0 To b - 1
        DataGridView1.Rows.Add(arrayCopy(i, 0), arrayCopy(i, 1))
    Next
End Sub

Private Sub btnArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArray.Click
    Dim id, name As String
    id = txtId.Text
    name = txtName.Text
    arrayCopy(b, 0) = id
    arrayCopy(b, 1) = name
    b += 1
End Sub
End Class
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.