Option Strict Off
Imports System.Data.SqlClient
Public Class 型體控制表
    ReadOnly conn As New SqlConnection
    ReadOnly da As New SqlDataAdapter
    ReadOnly cmd As New SqlCommand
    ReadOnly ds As New DataSet : ReadOnly ds1 As New DataSet
    Dim dr As SqlDataReader
    Private Sub Set_DGV1載入前設定()
        DataGridView1.DataSource = Nothing : ds.Clear()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView1.ColumnHeadersHeight = 25
        DataGridView1.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV1載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds) : DataGridView1.DataSource = ds.Tables(0) : conn.Close() : Set_grid()
    End Sub
    Private Sub Set_清單()
        Set_DGV1載入前設定()
        If PA = "" Then
            SQL1 = "SELECT 客戶簡稱 FROM 客戶控制表 ORDER BY 客戶簡稱"
        Else
            SQL1 = "SELECT 客戶簡稱 FROM 客戶控制表 WHERE (客戶簡稱 LIKE N'%" & PA & "%') ORDER BY 客戶簡稱"
        End If
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 150
    End Sub
    Private Sub Set_DGV2載入前設定()
        DataGridView2.DataSource = Nothing : ds1.Clear()
        DataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        DataGridView2.ColumnHeadersHeight = 25
        DataGridView2.AllowUserToAddRows = False
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
    End Sub
    Private Sub Set_DGV2載入後設定()
        cmd.Connection = conn : cmd.CommandText = SQL1 : da.SelectCommand = cmd : da.Fill(ds1) : DataGridView2.DataSource = ds1.Tables(0) : conn.Close() : Set_grid1()
    End Sub
    Private Sub Set_清單1()
        Set_DGV2載入前設定()
        SQL1 = "SELECT 客戶簡稱 AS 項次, 型體號碼, 型體名稱 FROM 型體控制表 WHERE (客戶簡稱 LIKE N'" & TextBox1.Text & "') ORDER BY 客戶簡稱"
        Set_DGV2載入後設定()
    End Sub
    Private Sub Set_grid1()
        DataGridView2.Columns(0).Width = 60 : DataGridView2.Columns(1).Width = 250 : DataGridView2.Columns(2).Width = 350
        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            DataGridView2(0, i).Value = i + 1
        Next
    End Sub
    Private Sub 型體控制表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = True
        TextBox1.Enabled = False : TextBox4.Visible = False : TextBox5.Visible = False
        Set_清單()
        If CC(15) = False Then : Button4.Enabled = False : Button5.Enabled = False : End If
        If CC(16) = False Then : Button3.Enabled = False : End If
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox1.Text = DataGridView1(0, e.RowIndex).Value.ToString
            Set_清單1()
        End If
    End Sub
    Private Sub DataGridView2_CellClick(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        If e.RowIndex = -1 Then : Else
            TextBox2.Text = DataGridView2(1, e.RowIndex).Value.ToString : TextBox3.Text = DataGridView2(2, e.RowIndex).Value.ToString
            TextBox4.Text = DataGridView2(1, e.RowIndex).Value.ToString : TextBox5.Text = DataGridView2(2, e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        清單修改.Close() : 客戶清單修改 = True : 清單修改.ShowDialog() : 客戶清單修改 = False : Set_清單()
    End Sub
    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        PA = InputBox("請輸入要查詢的關鍵字") : Set_清單() : PA = ""
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PA = InputBox("請輸入要查詢的關鍵字")
        Set_DGV2載入前設定()
        SQL1 = "SELECT 客戶簡稱 AS 項次, 型體號碼, 型體名稱 FROM 型體控制表 WHERE (客戶簡稱 LIKE N'" & TextBox1.Text & "') AND (型體號碼 LIKE N'%" & PA & "%' OR 型體名稱 LIKE N'%" & PA & "%') ORDER BY 型體號碼 DESC"
        Set_DGV2載入後設定()
        PA = ""
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("資料有缺或有誤，請重新檢查")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 客戶簡稱, 型體號碼, 型體名稱 FROM 型體控制表 WHERE 客戶簡稱 LIKE N'" & TextBox1.Text & "' AND 型體號碼 LIKE N'" & TextBox2.Text & "' AND 型體名稱 LIKE N'" & TextBox3.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                MsgBox("資料重複，請從新輸入或檢查")
                conn.Close() : dr.Close()
            Else
                conn.Close() : dr.Close()
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 型體控制表 (客戶簡稱, 型體號碼, 型體名稱) VALUES (N'" & TextBox1.Text & "', N'" & TextBox2.Text & "', N'" & TextBox3.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
                MsgBox("新增完成")
                conn.Close() : dr.Close()
            End If
        End If
        Set_清單1()
    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Then
            MsgBox("資料有缺或有誤，請重新檢查")
        Else
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 型體控制表 SET 型體號碼 = N'" & TextBox2.Text & "', 型體名稱 = N'" & TextBox3.Text & "' " &
                   "WHERE 客戶簡稱 LIKE N'" & TextBox1.Text & "' AND 型體號碼 LIKE N'" & TextBox4.Text & "' AND 型體名稱 LIKE N'" & TextBox5.Text & "'"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
            MsgBox("修改完成")
            conn.Close()
        End If
        Set_清單1()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        SQL1 = "DELETE FROM 型體控制表 WHERE 客戶簡稱 LIKE N'" & TextBox1.Text & "' AND 型體號碼 LIKE N'" & TextBox2.Text & "' AND 型體名稱 LIKE N'" & TextBox3.Text & "'"
        cmd.CommandText = SQL1 : cmd.Connection = conn
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : MsgBox("刪除完成") : End If
        conn.Close()
        Set_清單1() : Set_grid()
    End Sub
End Class