Option Strict Off Imports System.Data.SqlClient Public Class 清單修改2 ReadOnly conn As New SqlConnection ReadOnly da As New SqlDataAdapter ReadOnly cmd As New SqlCommand ReadOnly ds 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() End Sub Private Sub Set_資料清單() Set_DGV1載入前設定() If INVOICE用客戶資料 = True Then : SQL1 = "SELECT ATTN, TEL FROM INVOICE用客戶資料 ORDER BY ATTN" End If Set_DGV1載入後設定() End Sub Private Sub Set_grid() DataGridView1.Columns(0).Width = 188 : DataGridView1.Columns(1).Width = 187 End Sub Private Sub 清單修改2_Load(sender As Object, e As EventArgs) Handles MyBase.Load FormBorderStyle = FormBorderStyle.SizableToolWindow Set_資料清單() : Set_grid() 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 : TextBox2.Text = DataGridView1(1, e.RowIndex).Value.ToString TextBox3.Text = DataGridView1(0, e.RowIndex).Value.ToString End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If TextBox1.Text = "" Then MsgBox("資料有缺或有誤,請重新檢查") Else If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If If INVOICE用客戶資料 = True Then : SQL1 = "SELECT ATTN, TEL FROM INVOICE用客戶資料 WHERE ATTN LIKE N'" & TextBox1.Text & "'" End If 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 If INVOICE用客戶資料 = True Then : SQL1 = "INSERT INTO INVOICE用客戶資料 (ATTN, TEL) VALUES (N'" & TextBox1.Text & "', N'" & TextBox2.Text & "')" End If cmd.Connection = conn : cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() MsgBox("新增完成") conn.Close() : dr.Close() End If End If Set_資料清單() : Set_grid() End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click If TextBox1.Text = "" Then MsgBox("資料有缺或有誤,請重新檢查") Else If conn.State = ConnectionState.Closed Then : conn.ConnectionString = ConString : conn.Open() : End If If INVOICE用客戶資料 = True Then : SQL1 = "UPDATE INVOICE用客戶資料 SET ATTN = N'" & TextBox1.Text & "', TEL = N'" & TextBox2.Text & "' WHERE ATTN LIKE N'" & TextBox3.Text & "'" End If cmd.CommandText = SQL1 : cmd.ExecuteNonQuery() MsgBox("修改完成") conn.Close() End If Set_資料清單() : Set_grid() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click If INVOICE用客戶資料 = True Then : SQL1 = "DELETE FROM INVOICE用客戶資料 WHERE (ATTN = N'" & TextBox1.Text & "')" End If 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_資料清單() : Set_grid() End Sub End Class