Option Strict Off
Imports System.Data.Odbc

Public Class frmSettingNilaiMax
    Dim rd2 As OdbcDataReader
    Dim cKode_Kls, cBltNilai, cBobot As String
    Dim baris, jmlKriteria As Byte
    Dim strkode_Kriteria() As String
    Sub TampilPenilaian()
        DGV.Columns.Clear()
        DGV.Columns.Add("kode", "Kode Kriteria")
        DGV.Columns.Add("kode", "Nama Kriteria")
        DGV.Columns.Add("kode", "Nilai Maximal")
        Dim lstkode_Kriteria As New List(Of String)
        Dim cNilai As String = ""
        Dim sql As String = "Select nama_kriteria,kode_kriteria from tbkriteria order by kode_kriteria"
        Dim cmd As New OdbcCommand(sql, Conn)
        Dim rd As OdbcDataReader = cmd.ExecuteReader
        While rd.Read
            lstkode_Kriteria.Add(rd(1))
        End While
        strkode_Kriteria = lstkode_Kriteria.ToArray
        rd.Close()

        sql = "select Kode_kriteria,nama_kriteria from tbkriteria  order by kode_kriteria "
        cmd = New OdbcCommand(sql, Conn)
        rd = cmd.ExecuteReader
        Dim i As Integer = 0
        While rd.Read
            DGV.Rows.Add()
            DGV.Item(0, i).Value = rd(0)
            DGV.Item(1, i).Value = rd(1)

            i += 1
        End While
        rd.Close()

        sql = "select * from tbkriteria "
        cmd = New OdbcCommand(sql, Conn)
        rd = cmd.ExecuteReader
        If rd.HasRows Then
            rd.Close()
            For j As Integer = 0 To DGV.RowCount - 2
                sql = "select `nilai_max` from tbkriteria "
                cmd = New OdbcCommand(sql, Conn)
                rd = cmd.ExecuteReader
                i = 0
                While rd.Read
                    cmd = New OdbcCommand("select sub_kriteria from tbsub_kriteria where kode_sub='" & rd(0) & "' ", Conn)
                    rd2 = cmd.ExecuteReader
                    rd2.Read()
                    If rd2.HasRows = True Then
                        cNilai = rd2.Item(0)
                    End If
                    DGV.Item(2, i).Value = cNilai
                    i += 1
                End While
                rd.Close()
            Next
        Else
            rd.Close()
        End If

        For Each col As DataGridViewColumn In DGV.Columns
            col.SortMode = DataGridViewColumnSortMode.Programmatic
        Next
        DGV.AllowUserToAddRows = False
        DGV.Columns(0).Width = 60
        DGV.Columns(1).Width = 190
        DGV.Columns(2).Width = 90
        DGV.Columns(0).ReadOnly = True
        DGV.Columns(1).ReadOnly = True
        DGV.GridColor = Color.Blue
        DGV.DefaultCellStyle.ForeColor = Color.Blue
        DGV.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        DGV.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Focus()
        DGV.CurrentCell = DGV.Rows(0).Cells(2)
    End Sub

    Private Sub frmSettingNilaiMin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call TampilPenilaian()
    End Sub

    Private Sub DGV_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
        '  On Error Resume Next
        If e.ColumnIndex > 1 Then
            If e.RowIndex < DGV.RowCount Then
                Dim cmb As New DataGridViewComboBoxCell
                cmb.Items.Clear()
                Dim sql As String = "select `sub_kriteria` from `tbsub_kriteria` where `kode_kriteria`='" & strkode_Kriteria(e.RowIndex) & "' order by `kode_kriteria` asc"
                Dim cmd As New OdbcCommand(sql, Conn)
                Dim rd As OdbcDataReader = cmd.ExecuteReader
                While rd.Read
                    cmb.Items.Add(rd(0))
                End While
                rd.Close()
                DGV.Item(e.ColumnIndex, e.RowIndex) = cmb
            End If
        End If
    End Sub
    Sub SimpanPenilaian()
        Dim terisi As Boolean = True
        Dim cKode_Sub As String
        cKode_Sub = ""

        For j As Integer = 0 To DGV.RowCount - 1
            If DGV.Item(2, j).Value = "" Then
                terisi = False
            End If
        Next
      
        If terisi Then
           
            For j As Integer = 0 To DGV.RowCount - 1
                cmd = New OdbcCommand("select kode_sub from tbsub_kriteria where sub_kriteria='" & DGV.Item(2, j).Value & "' " & _
                         "and kode_kriteria =  '" & DGV.Rows(j).Cells(0).Value & "'", Conn)
                rd2 = cmd.ExecuteReader
                rd2.Read()
                If rd2.HasRows = True Then
                    cKode_Sub = rd2.Item(0)

                End If
                cmd = New OdbcCommand("Select * from tbkriteria where kode_kriteria='" & DGV.Rows(j).Cells(0).Value & "'", Conn)
                rd = cmd.ExecuteReader
                rd.Read()
                If rd.HasRows Then
                    Dim sqledit As String = "Update tbkriteria set " & _
                    "nilai_max='" & cKode_Sub & "' " & _
                    " where kode_kriteria='" & DGV.Rows(j).Cells(0).Value & "'"
                    cmd = New OdbcCommand(sqledit, Conn)

                End If
                cmd.ExecuteNonQuery()
            Next
           
            btnSimpan.Enabled = False
        Else
            MsgBox("Data belum terisi semua!")
            btnSimpan.Enabled = True
        End If
    End Sub
    Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick

    End Sub

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

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        Call SimpanPenilaian()
    End Sub
End Class