Option Strict Off
Imports System.Data.Odbc

Public Class frmNilaiPrioritasKriteria
    Dim strKode_Kriteria() As String
    Private Sub frmNilaiPrioritasKriteria_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call loading()
    End Sub

    Sub loading()
        Koneksi()
        Dim sql As String = "select A.`nama_kriteria`,C.`hasil`,C.Kode_sub from `tbkriteria` A, `tbprioritas` C " & _
            "where A.Kode_kriteria=C.Kode_sub  order by A.Kode_Kriteria asc"
        Dim cmd As New OdbcCommand(sql, Conn)
        Dim rd As OdbcDataReader = cmd.ExecuteReader
        Dim lstHasil As New List(Of Double)
        Dim lstkode_Kriteria As New List(Of String)
        Dim i As Integer = 0
        If Not rd.HasRows Then
            MsgBox("Data Tidak Ada!")
            Exit Sub
        End If
        While rd.Read
            lstkode_Kriteria.Add(rd("kode_sub"))
            dg1.Columns.Add("", "")
            dg1.Rows.Add(2)
            dg1.Item(i, 0).Value = rd(0)
            dg1.Rows(0).DefaultCellStyle.BackColor = Color.Gray
            dg1.Rows(0).DefaultCellStyle.ForeColor = Color.White
            lstHasil.Add(rd(1))
            i += 1
        End While
        strkode_Kriteria = lstkode_Kriteria.ToArray
        rd.Close()

        i = 0
        While i < lstHasil.Count
            dg1.Item(i, 1).Value = Format(lstHasil(i), "0.00")
            dg1.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            i += 1
        End While
        'dg1.Rows.Add(i)

        i = 0
        Dim Jml_Sub As Integer = 0
        While i < lstHasil.Count
            sql = "select count(*) as Jml from tbsub_kriteria " & _
            "where  `kode_kriteria`='" & strKode_Kriteria(i) & "' group by kode_Kriteria"
            cmd = New OdbcCommand(sql, Conn)
            rd2 = cmd.ExecuteReader
            If rd2.HasRows Then
                If Jml_Sub <= rd2.Item(0) Then Jml_Sub = rd2.Item(0)
            End If
            i += 1
        End While
        dg1.Rows.Add(Jml_Sub)
        i = 0
        While i < lstHasil.Count
            'sql = "select `hasil`.`subkriteria`,`hasil`.`hasil` from `hasil`,`subkriteria` " & _
            '"where `hasil`.`subkriteria`=`subkriteria`.`sub kriteria` and " & _
            '"`hasil`.`kriteria`='" & dg1.Item(i, 0).Value & "' and " & _
            '"`subkriteria`.`kriteria`='" & dg1.Item(i, 0).Value & "' order by `subkriteria`.`urutan` asc"
            sql = "select B.`sub_kriteria`,C.`hasil` from tbsub_kriteria B, `tbprioritas` C " & _
            "where  C.Kode_Sub = B.Kode_Sub  and C.`kode_kriteria`='" & strKode_Kriteria(i) & "' and  " & _
            "B.`kode_kriteria`='" & strKode_Kriteria(i) & "' order by c.Kode_kriteria,c.kode_sub asc"
            cmd = New OdbcCommand(sql, Conn)
            rd = cmd.ExecuteReader
            Dim j As Integer = 1

            While rd.Read

                dg1.Item(i, j * 2 + 1).Value = rd(0)
                dg1.Rows(j * 2 + 1).DefaultCellStyle.BackColor = Color.Gray
                dg1.Rows(j * 2 + 1).DefaultCellStyle.ForeColor = Color.White
                dg1.Item(i, j * 2 + 2).Value = Format(rd(1), "0.00")
                j += 1
                '  
            End While

            rd.Close()
            i += 1
        End While

        For Each col As DataGridViewColumn In dg1.Columns
            col.SortMode = DataGridViewColumnSortMode.Programmatic
        Next

    End Sub

    Private Sub dg1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dg1.CellBeginEdit
        e.Cancel = True
    End Sub

    Private Sub dg1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg1.CellContentClick

    End Sub

    Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
        Me.Dispose()
    End Sub
End Class