Option Explicit On
Option Strict On
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Public Class frmAddStudent
Dim Conn As New OleDbConnection()
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Dim dv As New DataView
Dim btnChk, curcode As String
Dim comStudent As OleDbCommand
Dim chkFillter As Boolean = False
Private Sub frmAddStudent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
ShowData()
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
Me.Close()
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
Dim sqltxt As String = ""
Dim comtxt As New OleDbCommand
Dim result As String
Dim level As String = ""
If p1.Checked Then
level = "1"
ElseIf p2.Checked Then
level = "2"
ElseIf p3.Checked Then
level = "3"
ElseIf p4.Checked Then
level = "4"
ElseIf p5.Checked Then
level = "5"
Else
level = "6"
End If
sqltxt = "delete student set code ='"
sqltxt = sqltxt & txtcode.Text & "',code='"
sqltxt = sqltxt & txtyear.Text & "',year='"
sqltxt = sqltxt & txtfullname.Text & "',fullname='"
sqltxt = sqltxt & level & "',level='"
sqltxt = sqltxt & " where code='" & curcode & "'"
result = MessageBox.Show("ต้องการลบข้อมูล", "Warnning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning).ToString
If (result = "OK") Then
With comtxt
.CommandType = CommandType.Text
.CommandText = sqltxt
.Connection = Conn
.ExecuteNonQuery()
End With
btnSwitchOff()
End If
End Sub
Private Sub btnSwitchOn()
cmdSave.Enabled = True
cmdCancel.Enabled = True
cmdClose.Enabled = True
cmdAdd.Enabled = False
cmdEdit.Enabled = True
cmdDelete.Enabled = False
End Sub
Private Sub btnSwitchOff()
cmdSave.Enabled = False
cmdCancel.Enabled = False
cmdClose.Enabled = True
cmdAdd.Enabled = True
cmdEdit.Enabled = False
cmdDelete.Enabled = False
End Sub
Private Sub cmdedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdedit.Click
btnChk = "edit"
btnSwitchOn()
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
btnSwitchOn()
btnChk = "add"
txtcode.Clear()
txtyear.Clear()
txtfullname.Clear()
If txtcode.Text.Trim = "" Then
txtcode.Focus()
Else
p1.Focus()
End If
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
btnSwitchOff()
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim sqltxt As String = ""
Dim level As String = ""
Dim comtxt As New OleDbCommand
btnSwitchOff()
If txtcode.Text.Trim <> "" Then
If p1.Checked Then
level = "1"
ElseIf p2.Checked Then
level = "2"
ElseIf p3.Checked Then
level = "3"
ElseIf p4.Checked Then
level = "4"
ElseIf p5.Checked Then
level = "5"
Else
level = "6"
End If
If btnChk = "add" Then
sqltxt = "insert into student(code,year,fullname,level) values('"
sqltxt = sqltxt & txtcode.Text & "','"
sqltxt = sqltxt & txtyear.Text & "','"
sqltxt = sqltxt & txtfullname.Text & "','"
sqltxt = sqltxt & level & "','"
ElseIf btnChk = "edit" Then
sqltxt = "update student set code ='"
sqltxt = sqltxt & txtcode.Text & "','"
sqltxt = sqltxt & txtyear.Text & "','"
sqltxt = sqltxt & txtfullname.Text & "','"
sqltxt = sqltxt & level & "','"
sqltxt = sqltxt & " where code = '" & curcode & "'"
End If
With comtxt
.CommandType = CommandType.Text
.CommandText = sqltxt
.Connection = Conn
.ExecuteNonQuery()
End With
btnSwitchOff()
Else
MessageBox.Show("กรอกข้อมูลไม่ครบ", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtcode.Focus()
End If
End Sub
Private Sub ShowData()
Dim sqlYear As String = "select * from student"
da = New OleDbDataAdapter(sqlYear, Conn)
ds.Clear()
da.Fill(ds, "student")
If ds.Tables("student").Rows.Count <> 0 Then
dv = New DataView(ds.Tables("student"))
With dgvStudent
.DataSource = dv
End With
DgvFormat()
End If
End Sub
Private Sub DgvFormat()
With dgvStudent
.Columns(0).Width = 70
.Columns(0).HeaderText = "รหัส"
.Columns(1).Width = 100
.Columns(1).HeaderText = "เข้าศึกษา"
.Columns(2).Width = 220
.Columns(2).HeaderText = "ชื่อ - สกุล"
.Columns(3).Width = 130
.Columns(3).HeaderText = "ชั้น"
End With
End Sub
Private Sub dgvStudent_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStudent.CellContentClick
Dim level As String = ""
If e.RowIndex > -1 Then
With dgvStudent
txtcode.Text = .Rows.Item(e.RowIndex).Cells(0).Value.ToString()
txtyear.Text = .Rows.Item(e.RowIndex).Cells(1).Value.ToString()
txtfullname.Text = .Rows.Item(e.RowIndex).Cells(2).Value.ToString
level = .Rows.Item(e.RowIndex).Cells(3).Value.ToString()
If level = "1" Then
p1.Checked = True
ElseIf level = "2" Then
p2.Checked = True
ElseIf level = "3" Then
p3.Checked = (True)
ElseIf level = "4" Then
p4.Checked = (True)
ElseIf level = "5" Then
p5.Checked = (True)
ElseIf level = "6" Then
p6.Checked = (True)
End If
level = .Rows.Item(e.RowIndex).Cells(3).Value.ToString()
End With
cmdDelete.Enabled = True
cmdedit.Enabled = True
End If
End Sub
End Class
Dim sqltxt As String = ""
Dim level As String = ""
Dim comtxt As New OleDbCommand
btnSwitchOff()
If txtcode.Text.Trim <> "" Then
If p1.Checked Then
level = "ป.1"
Else
If p2.Checked Then
level = "ป.2"
Else
If p3.Checked Then
level = "ป.3"
Else
If p4.Checked Then
level = "ป.4"
Else
If p5.Checked Then
level = "ป.5"
Else
If p6.Checked Then
level = "ป.6"
End If
End If
End If
End If
End If
End If
If btnChk = "add" Then
sqltxt = "insert into [student]([code],[year],[fullname],[level]) values('"
sqltxt = sqltxt & txtcode.Text & "','"
sqltxt = sqltxt & txtyear.Text & "','"
sqltxt = sqltxt & txtfullname.Text & "','"
sqltxt = sqltxt & level & "')"
ElseIf btnChk = "edit" Then
sqltxt = "UPDATE [student] SET [code] ='" & txtcode.Text & "',"
sqltxt &= "[year] = '" & txtyear.Text & "',"
sqltxt &= "[fullname] ='" & txtfullname.Text & "' WHERE ([id] ="
sqltxt &= CInt(txtid.Text) & ")"
MessageBox.Show(sqltxt)
End If
With comtxt
.CommandType = CommandType.Text
.CommandText = sqltxt
.Connection = Conn
.ExecuteNonQuery()
End With
btnSwitchOff()
Else
MessageBox.Show("ไม่ได้กรอกปีที่จัดกิจกรรม", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtcode.Focus()
End If
ShowData()
End Sub
Private Sub ShowData()
Dim sqlStudent As String = "select * from student"
da = New OleDbDataAdapter(sqlStudent, Conn)
ds.Clear()
da.Fill(ds, "student")
If ds.Tables("student").Rows.Count <> 0 Then
dv = New DataView(ds.Tables("student"))
With dgvStudent
.DataSource = dv
End With
DgvFormat()
End If