Soni Gunz Soni Gunz - 7 months ago 29
Vb.net Question

Binding a 2D array into a Datagrid

So my case is that I want to populate a 2d array from two fields in my database.
I've got a problem with assigning an array with a value which is a database field
and the second one is displaying the array in datagrid, i've coded dgv2.Datasource = myArray but it's still returning an error message.

This is my code:

Dim msql As String = "select item_id from detail"
Dim arayT(,) As String

CMD = New MySqlCommand(msql, conn.konek)

Try
Dim res= CMD.ExecuteReader()


While res.Read()
For i As Integer = 0 To x - 1
For j = 0 To y - 1

arayT(i, j) = res.GetString("item_id")


Next j
Next i
End While

Catch ex As Exception
MessageBox.Show("ERROR")

End Try
dgv2.DataSource = arayT


So my question is
first, how to assign record column from database as value into my 2d array? (i'm still not sure my coding is right)
second, how to view the 2d array into datagrid, i mean binding 2d array into datagrid

A few requests from me... please don't ask me to use a list of(t) or use LINQ cause I'm still not too familiar with them.

i have done some fix in my code but it didn't worked nicely..here is my latest screenshot
enter image description here

Answer

hi guys my questions is finally clear, this is the answer to my question

ReDim Preserve arayT(x, y)
For i = 0 To (x-1)
    For j = 0 To (y-1)
        hasil.Read()
        arayT(i, j) = hasil("kode_brg")
    Next j
 Next i

it turned out that i don't need to use while..

many thanks for Afnan, Neolisk and Emmad that had already helped me to answer my questions. :)