
Imports System.Data
Imports System.Data.OleDb
Public Class _11
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gSQL As String
    Private Sub _11_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call connectdatabase()
        Call SetGrid()
        Call Showdata()
    End Sub
    Private Sub SetGrid()
        If dgvem.RowCount > 0 Then
            dgvem.Columns(0).HeaderText = "รหัสหน่วยงาน"
            dgvem.Columns(1).HeaderText = "ชื่อหน่วยงาน"
            dgvem.Columns(1).Width = 150
            dgvem.Columns(2).HeaderText = "จังหวัดหน่วยงาน"
            dgvem.Columns(3).HeaderText = "เบอร์โทรศัพท์หน่วยงาน"
            dgvem.Columns(3).Width = 120
            dgvem.Columns(4).HeaderText = "อีเมล์หน่วยงาน"
            dgvem.Columns(5).HeaderText = "ที่อยู่หน่วยงาน"
            dgvem.Columns(6).HeaderText = "ชื่อผู้ติดต่อ"
            dgvem.Columns(7).HeaderText = "ตำแหน่งผู้ติดต่อ"
            dgvem.Columns(8).HeaderText = "ราคา"
        End If
    End Sub
    Private Sub Showdata()
        Dim dt As New DataTable
        gSQL = "Select Ins_ID,Ins_name,Ins_country,Ins_tel,Ins_email,Ins_address,Ins_contact,Ins_CTstatus,Ins_price  From Institution_Train"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader
            If dr.HasRows Then
                dt.Load(dr)
                dgvem.DataSource = dt
                SetGrid()
            End If
            dr.Close()
        End With
    End Sub
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        Dim dt As New DataTable
        gSQL = "Select Ins_ID,Ins_name,Ins_country,Ins_tel,Ins_email,Ins_address,Ins_contact,Ins_CTstatus,Ins_price  From Institution_Train  Where Ins_name Like '%" & txtSearch.Text & "%'"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gSQL
            .Connection = cnn
            dr = .ExecuteReader
            If dr.HasRows Then
                dt.Load(dr)
                dgvem.DataSource = dt
                SetGrid()
            End If
            dr.Close()
        End With
    End Sub

    Private Sub dgvem_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvem.CellMouseDoubleClick
        If e.RowIndex >= 0 Then
            addem.TextBox6.Text = dgvem.Rows(e.RowIndex).Cells(1).Value
            addem.TextBox5.Text = dgvem.Rows(e.RowIndex).Cells(8).Value
            Me.Close()
        End If
    End Sub
End Class