Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmHome
Private Sub frmHome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindDataGrid()
End Sub
Private Sub BindDataGrid()
Dim myConnection As SqlCeConnection
Dim dt As New DataTable
Dim Adapter As SqlCeDataAdapter
myConnection = New SqlCeConnection("Data Source=D:\โปรแกรมตำรวจท่องเที่ยว\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT [id], [name], [call], [jan], [fab], [march], [april], [may], [june], [july], [august], [sep], [oct], [novem], [decem], [total], [note] FROM [mytable]"
myCommand.CommandType = CommandType.Text
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(dt)
myConnection.Close()
Me.dgName.DataSource = dt
Me.dgName.Columns.Clear()
Dim column As DataGridViewTextBoxColumn
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.dgName.Columns.Add(ColumnCheckBox)
Dim ckBox As New CheckBox()
Dim rect As Rectangle = dgName.GetCellDisplayRectangle(0, -1, True)
ckBox.Size = New Size(18, 18)
rect.X = rect.Location.X + (rect.Width / 4)
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, New EventHandler(AddressOf ckBox_CheckedChanged)
Me.dgName.Controls.Add(ckBox)
dgName.Columns(0).Frozen = False
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "id"
column.HeaderText = "CustomerID"
column.Width = 100
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Name"
column.HeaderText = "Name"
column.Width = 100
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "call"
column.HeaderText = "Callsign"
column.Width = 80
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "fab"
column.HeaderText = "fabruary"
column.Width = 60
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "march"
column.HeaderText = "march"
column.Width = 60
Me.dgName.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "april"
column.HeaderText = "april"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "may"
column.HeaderText = "may"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "june"
column.HeaderText = "june"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "july"
column.HeaderText = "july"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "august"
column.HeaderText = "august"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "sep"
column.HeaderText = "sep"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "oct"
column.HeaderText = "oct"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "novem"
column.HeaderText = "novem"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "decem"
column.HeaderText = "decem"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "total"
column.HeaderText = "Total"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "note"
column.HeaderText = "Note"
column.Width = 60
Me.dgName.Columns.Add(column)
dt = Nothing
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Me.Hide()
Dim f As New frmAdd
f.Show()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Me.Hide()
Dim f As New frmEdit()
f._strID = Me.dgName(0, Me.dgName.CurrentCell.RowIndex).Value.ToString()
f.Show()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
If MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Dim strID As String = Me.dgName(0, Me.dgName.CurrentCell.RowIndex).Value.ToString()
Dim myConnection As SqlCeConnection
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=D:\โปรแกรมตำรวจท่องเที่ยว\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "DELETE FROM [mytable] WHERE id = '" & strID & "'"
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Delete Successfully")
BindDataGrid()
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Hide()
Dim f As New frmMain
f.Show()
End Sub
Private Sub btnToS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToS.Click
Me.Hide()
Dim f As New frmSearch
f.Show()
End Sub
Private Sub ckBox_CheckedChanged()
Dim i As Integer = 0
For j As Integer = 0 To Me.dgName.RowCount - 1
Me.dgName(0, j).Value = True
Next
End Sub
Private Function ColumnCheckBox() As String
Throw New NotImplementedException
End Function
End Class
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmAdd
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtCall.Text = "" Then
MessageBox.Show("Please input (callsign)")
Me.txtCall.Focus()
Exit Sub
End If
If Me.txtTotal.Text = "" Then
MessageBox.Show("Please input (total)")
Me.txtTotal.Focus()
Exit Sub
End If
If Me.txtNote.Text = "" Then
MessageBox.Show("Please input (Note)")
Me.txtNote.Focus()
Exit Sub
End If
Dim myConnection As SqlCeConnection
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=D:\โปรแกรมตำรวจท่องเที่ยว\WindowsFormsApplication\WindowsFormsApplication\Database1.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "INSERT INTO [mytable] ([name], [callsign], [total], [note]) VALUES " & _
" ('" & Me.txtName.Text & "','" & Me.txtCall.Text & "','" & Me.txtTotal.Text & "','" & Me.txtNote.Text & "' ) "
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Save Successfully.")
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
End Class