Reputation: 135
hi guys i have two field in my database. "kode_faktur" as transaction id and "kode_brg" as item-id.. these two fields are exist in my detail transaction table.. so let's says 1 transaction id could have a different number of item-id.. e.g, in my ss below "kode_faktur" 1 has 5 item-id(t1, k4,k9,c2,m4) and "kode_faktur" 2 has 4 item-id(m2,l1,f2,d1)
in my ss below i've already succeed in creating a 2d array (872 rows and 20 columns), where 20 columns is the number that i've acquired from the max number of item-id that each transaction could have
i'm using for next looping in this case, i'm using two for next looping in this case, i've succeed in looping the first transaction, but the problem is = outer of my for looping didn't worked. the outer for loops infinite..
for more specific i've included ss below that will explain everything
my requirements here are like this
transforming two field into sparse matrix using 2d array
binding the 2d array into datagrid
i've already solved the second task. why i'm doing this? because i want to compare this sparse matrix array with another 1d array, create a new one from them, displaying it with grid.
thanks for your attention
regards,me
this is the full code
'FILL X
Dim nil_x As String = "SELECT max( kode_faktur ) FROM detail"
Dim x As Int32
Dim CMD_X = New MySqlCommand(nil_x, conn.konek)
x = Convert.ToInt32(CMD_X.ExecuteScalar())
'FILL Y
Dim nil_y As String = "select max(x.jumlah) from (select count(*) as jumlah from detail group by kode_faktur)x"
Dim y As Int32
CMD_Y = New MySqlCommand(nil_y, conn.konek)
y = Convert.ToInt32(CMD_Y.ExecuteScalar())
'LOOPING ARRAY TRANS
Dim i, j As Integer
Dim arayT(,) As String
Dim hasil As MySqlDataReader
ReDim arayT(x, y)
For i = 0 To x
Dim msql As String = "select kode_brg from detail where kode_faktur = '" & i & "' "
CMD = New MySqlCommand(msql, conn.konek)
hasil = CMD.ExecuteReader()
Dim row_y As String = "select count(kode_brg) from detail where kode_faktur = '" & i & "' "
Dim y2 As Int32
CMD_Y2 = New MySqlCommand(row_y, conn.konek)
y2 = Convert.ToInt32(CMD_Y2.ExecuteScalar())
If hasil.HasRows Then
For j = 0 To y2 - 1
hasil.Read()
arayT(i, j) = hasil("kode_brg")
Next j
Else
Continue For
Exit For
End If
'Exit For
Next i
conn.konek.Close()
this is the ss
Upvotes: 1
Views: 3054
Reputation: 9991
To manually create a Sparse matrix you could do it like this:
Dim matrix As Integer()() = New Integer(4)() {}
matrix(0) = New Integer() {1, 2, 0, 0, 0, 0, 0}
matrix(1) = New Integer() {0, 3, 4, 0, 0, 0, 0}
matrix(2) = New Integer() {0, 0, 5, 6, 7, 0, 0}
matrix(3) = New Integer() {0, 0, 0, 0, 0, 8, 0}
matrix(4) = New Integer() {0, 0, 0, 0, 0, 0, 9}
To create one programmatically:
Dim ROW_COUNT As Integer = 5
Dim COLUMN_COUNT As Integer = 7
Dim matrix As Integer()() = New Integer(ROW_COUNT - 1)() {}
For rowIndex As Integer = 0 To (ROW_COUNT - 1)
matrix(rowIndex) = New Integer(COLUMN_COUNT - 1) {}
For columnIndex As Integer = 0 To (COLUMN_COUNT - 1)
matrix(rowIndex)(columnIndex) = (10 Mod ((rowIndex + 1) + (columnIndex + 1)))
Next
Next
Result:
0, 1, 2, 0, 4, 3, 2
1, 2, 0, 4, 3, 2, 1
2, 0, 4, 3, 2, 1, 0
0, 4, 3, 2, 1, 0, 10
4, 3, 2, 1, 0, 10, 10
If you want to test two matrices for equality you can create a function like this:
Public Shared Function MatrixEquals(m1 As Integer()(), m2 As Integer()()) As Boolean
If (m1 Is Nothing) Then
Return (m2 Is Nothing)
End If
If (m2 Is Nothing) Then
Return False
End If
If (m1.Length <> m2.Length) Then
Return False
End If
For i As Integer = 0 To (m1.Length - 1)
If (Not m1(i).SequenceEqual(m2(i))) Then
Return False
End If
Next
Return True
End Function
Upvotes: 1