Option Strict Off
Imports System.Data.SqlClient
Public Class 部件清單控制表
    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 ds2 As New DataSet : Dim ds4 As New DataSet : Dim ds5 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載入前設定()
        SQL1 = "SELECT 部件, 貼標對應名稱,樣品單用選擇 FROM 部件清單 ORDER BY 部件"
        Set_DGV1載入後設定()
    End Sub
    Private Sub Set_grid()
        DataGridView1.Columns(0).Width = 200 : DataGridView1.Columns(1).Width = 100 : DataGridView1.Columns(2).Width = 100
    End Sub
    Private Sub ComboBox1下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 部件類別 FROM 部件大項分類 ORDER BY 部件類別"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox1.Items.Clear() : While (dr.Read()) : ComboBox1.Items.Add(dr("部件類別")) : End While : conn.Close()
    End Sub
    Private Sub ComboBox2下拉表單資料載入()
        conn.Close()
        If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
        SQL1 = "SELECT 貼標對應名稱 FROM 貼標對應名稱清單"
        cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
        ComboBox2.Items.Clear() : While (dr.Read()) : ComboBox2.Items.Add(dr("貼標對應名稱")) : End While : conn.Close()
    End Sub
    Private Sub 部件清單控制表_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FormBorderStyle = FormBorderStyle.SizableToolWindow : ControlBox = True
        TextBox1.Enabled = False : Button1.Enabled = False
        ComboBox2下拉表單資料載入() : ComboBox1下拉表單資料載入()
        Set_清單()
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox1.Text = Strings.Left(ComboBox1.Text, 2)
    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 = Strings.Left(DataGridView1.Rows(e.RowIndex).Cells("部件").Value, 2)
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "SELECT 部件類別 FROM 部件大項分類 WHERE 部件類別 LIKE N'" & TextBox1.Text & "%' ORDER BY 部件類別"
            cmd.CommandText = SQL1 : cmd.Connection = conn : dr = cmd.ExecuteReader
            If dr.Read() Then
                ComboBox1.Text = dr("部件類別")
            End If
            TextBox2.Text = Strings.Mid(DataGridView1.Rows(e.RowIndex).Cells("部件").Value, 3, 100)
            ComboBox2下拉表單資料載入()
            ComboBox2.Text = DataGridView1.Rows(e.RowIndex).Cells("貼標對應名稱").Value
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ComboBox2下拉表單資料載入()
    End Sub
    Private Sub Button29_Click(sender As Object, e As EventArgs) Handles Button29.Click
        ComboBox2下拉表單資料載入() : ComboBox1下拉表單資料載入()
        TextBox1.Text = "" : TextBox2.Text = ""
        Button29.Enabled = False : Button1.Enabled = True
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.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 & TextBox2.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 & TextBox2.Text & "', N'" & ComboBox2.Text & "')"
                cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery()
                MsgBox("新增完成") : Set_清單()
                conn.Close() : dr.Close()
                Button29.Enabled = True : Button1.Enabled = False
            End If
        End If
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        SQL1 = "DELETE FROM 部件清單 WHERE (部件 LIKE N'" & TextBox1.Text & TextBox2.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("刪除完成") : Set_清單() : End If
        conn.Close()
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            資料數 = DataGridView1.Rows.Count : MyModule1.進度條()
            If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If
            SQL1 = "UPDATE 部件清單 SET  樣品單用選擇 = N'" & DataGridView1.Rows(i).Cells("樣品單用選擇").Value & "' WHERE (部件 LIKE N'" & DataGridView1.Rows(i).Cells("部件").Value & "')"
            cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() : conn.Close()
        Next i : WINPROFIT_ERP_SYS.ToolStripProgressBar1.Value = 0 : 最終數 = 0
        MsgBox("存檔完成")
    End Sub
    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        For i As Integer = 0 To DataGridView1.Rows.Count - 1 : DataGridView1.Rows(i).Cells("樣品單用選擇").Value = True : Next i
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        For i As Integer = 0 To DataGridView1.Rows.Count - 1 : DataGridView1.Rows(i).Cells("樣品單用選擇").Value = False : Next i
    End Sub
End Class