Imports System.Data
Imports System.Data.OleDb
Public Class ShowCompuny
    Dim dr As OleDbDataReader
    Dim cmd As New OleDbCommand
    Dim gsql As String
    Private Sub ShowCompuny_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ConnectDatabase()
        showlist()
        SetGrid()
        SetHeaderGrid()
    End Sub

    Private Sub showlist() 'dataGridView
        Dim dt As New DataTable
        gsql = "select * from COMPANY"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gsql
            .Connection = cnn
            dr = .ExecuteReader
        End With
        If dr.HasRows Then
            dt.Load(dr)
            dgvcompany.DataSource = dt 'dtโหลด dr ให้ dgv=dt
        End If
        dr.Close()
    End Sub

    Private Sub SetHeaderGrid()
        'datagrid
        If dgvcompany.RowCount > 0 Then
            dgvcompany.Columns(0).HeaderText = "รหัสบริษัท"
            dgvcompany.Columns(0).Width = 100
            dgvcompany.Columns(1).HeaderText = "ชื่อบริษัท"
            dgvcompany.Columns(1).Width = 100
            dgvcompany.Columns(2).HeaderText = "เบอร์โทรศํพท์"
            dgvcompany.Columns(2).Width = 100
            dgvcompany.Columns(3).HeaderText = "ที่อยู่"
            dgvcompany.Columns(3).Width = 80
        End If

    End Sub

    Private Sub SetGrid() 'สร้างข้อความหัวคอลัมน์ แก้ไขหัวคอลัมน์ตามต้องการ
        If dgvcompany.Columns(0).HeaderText = "รหัสบริษัท" Then
            dgvcompany.Columns(0).Width = 100
            dgvcompany.Columns(1).HeaderText = "ชื่อบริษัท"
            dgvcompany.Columns(1).Width = 100
            dgvcompany.Columns(2).HeaderText = "เบอร์โทรศํพท์"
            dgvcompany.Columns(2).Width = 100
            dgvcompany.Columns(3).HeaderText = "ที่อยู่"
            dgvcompany.Columns(3).Width = 80



        End If
    End Sub

    Private Sub dgvcompany_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvcompany.CellContentClick
        Dim i As Integer 'ดับเบิ้ลคลิก
        With dgvcompany
            If e.RowIndex >= 0 Then
                i = .CurrentRow.Index
                FmOrder.txtCPYID.Text = .Rows(i).Cells(0).Value.ToString
                FmOrder.txtCPYName.Text = .Rows(i).Cells(1).Value.ToString
            End If
        End With
        Me.Close()

    End Sub

    Private Sub txtCPY_TextChanged(sender As Object, e As EventArgs) Handles txtCPY.TextChanged
        Dim dt As New DataTable
        gsql = "Select * from COMPANY"

        gsql += " where CPY_Name like '%" & txtCPY.Text '"
        With cmd
            .CommandType = CommandType.Text
            .CommandText = gsql
            .Connection = cnn
            dr = .ExecuteReader
            If dr.HasRows Then
                dt.Load(dr)
                dgvcompany.DataSource = dt
            End If
            dr.Close()
        End With
    End Sub
End Class