Option Strict Off
Imports System.Data.SqlClient
Public Class FACTORY_資料管理
    Dim conn As New SqlConnection
    Dim da As New SqlDataAdapter
    Dim cmd As New SqlCommand
    Dim ds As New DataSet : Dim ds1 As New DataSet
    Dim dr As SqlDataReader
    Dim OO As Boolean
    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()
    End Sub
    Private Sub Set_FACTORY清單()
        Set_DGV1載入前設定()
        SQL1 = "SELECT 工廠 AS Factory_Code, 供應商資料 AS Vendor_Name, 供應商地址 AS Ship_From_Name_Ship_From_Address FROM 工廠控制表"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid1()
        DataGridView1.Columns(0).Width = 125 : DataGridView1.Columns(1).Width = 300 : DataGridView1.Columns(2).Width = 885
    End Sub
    Private Sub FACTORY_資料管理_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = False
        Set_FACTORY清單() : Set_grid1()
    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 : TextBox3.Text = DataGridView1(1, e.RowIndex).Value.ToString
            TextBox2.Text = DataGridView1(2, e.RowIndex).Value.ToString
        End If
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Me.Close()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text <> "" Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 工廠 FROM 工廠控制表 WHERE 工廠 LIKE '" & TextBox1.Text & "'"
            cmd.Connection = conn : cmd.CommandText = SQL1 : dr = cmd.ExecuteReader
            If dr.Read() Then
                OO = True : MsgBox("資料重複") : conn.Close() : Exit Sub
            Else
                OO = False
            End If : conn.Close()
            If OO = False Then
                If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
                SQL1 = "INSERT INTO 工廠控制表 (工廠, 供應商資料, 供應商地址) VALUES ('" & TextBox1.Text & "', '" & TextBox3.Text & "', '" & TextBox2.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            End If
            Set_FACTORY清單() : Set_grid1()
            MsgBox("新增完成")
        Else
            MsgBox("Key index 不可空白")
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text <> "" Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 工廠控制表 SET 供應商資料 = '" & TextBox3.Text & "', 供應商地址 = '" & TextBox2.Text & "' WHERE (工廠 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
            Set_FACTORY清單() : Set_grid1()
            MsgBox("修改完成")
        Else
            MsgBox("Key index 不可空白")
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim aa As MsgBoxResult
        aa = MsgBox("確定要刪除該筆資料?", MsgBoxStyle.OkCancel)
        If aa = MsgBoxResult.Ok Then
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "DELETE FROM 工廠控制表 WHERE (工廠 = '" & TextBox1.Text & "')"
            cmd.CommandText = SQL1 : cmd.Connection = conn : cmd.ExecuteNonQuery() : cmd.ExecuteNonQuery() : conn.Close()
            Set_FACTORY清單() : Set_grid1()
            MsgBox("刪除完成")
        End If
    End Sub
End Class