Imports System.IO
Imports System.Data.DataTable
Public Class Form1
Dim table As New DataTable("table")
Dim index As Integer
Private dtTableGrd As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'หัว Columns
table.Columns.Add("ชื่อผู้ใช้บริการ", Type.GetType("System.String"))
table.Columns.Add("เบอร์โทรศัพท์", Type.GetType("System.String"))
table.Columns.Add("ประเภทรถ", Type.GetType("System.String"))
table.Columns.Add("บริการที่ใช้", Type.GetType("System.String"))
table.Columns.Add("ราคาบริการ", Type.GetType("System.String"))
table.Columns.Add("วันที่บริการ", Type.GetType("System.String"))
DataGridView1.DataSource = table
dtTableGrd = table
End Sub
'เพื่มข้อมูลเพื่มแถว
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
table.Rows.Add(ชื่อ.Text, เบอร์.Text, รถ.Text, บริการ.Text, ราคา.Text, วันที่.Text)
ชื่อ.Clear()
เบอร์.Clear()
ราคา.Clear()
DataGridView1.DataSource = table
End Sub
'Save
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
On Error GoTo SaveErr
Dim writer As New StreamWriter("C:\Memoryคาร์แคร์\SAVE\MemorySave.Txt")
For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
If j = DataGridView1.Columns.Count - 1 Then
writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "l")
Else
writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "l")
End If
Next j
writer.WriteLine("")
Next
writer.Close()
MessageBox.Show("บันทึกเรียบร้อย")
SaveErr:
End Sub
'ออกจากโปรแกรม
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Close()
End Sub
'DeleteRow
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
On Error GoTo SaveErr
Dim rowIndex = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(RightToLeftLayout)
SaveErr:
End Sub
'ค้นหา (ใช้ไม่ได้ครับได้แค่ผู้บริการ)
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
On Error GoTo SaveErr
dtTableGrd.DefaultView.RowFilter = "(Convert(ชื่อผู้ใช้บริการ, 'System.String') LIKE '" & ST.Text & "')" & _
"OR (เบอร์โทรศัพท์, 'System.String') LIKE '" & ST.Text & "')"
SaveErr:
End Sub
Private bitmap As Bitmap
'ปริ้น
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim height As Integer = DataGridView1.Height
DataGridView1.Height = DataGridView1.RowCount * DataGridView1.RowTemplate.Height
bitmap = New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)
DataGridView1.DrawToBitmap(bitmap, New Rectangle(1, 1, Me.DataGridView1.Width, Me.DataGridView1.Height))
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
PrintPreviewDialog1.ShowDialog()
DataGridView1.Height = height
End Sub
'ปริ้น
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(bitmap, 1, 1)
Dim rectPrint As RectangleF = e.PageSettings.PrintableArea
If Me.DataGridView1.Height - rectPrint.Height > 0 Then e.HasMorePages = True
End Sub
'Open
Private Sub เปดไฟลงานToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles เปดไฟลงานToolStripMenuItem.Click
Dim lines() As String
Dim data() As String
lines = File.ReadAllLines("C:\Memoryคาร์แคร์\SAVE\MemorySave.Txt")
For i As Integer = 0 To lines.Length - 1 Step +1
data = lines(i).ToString().Split("l")
Dim row(data.Length - 1) As String
For j As Integer = 0 To data.Length - 1 Step +1
row(j) = data(j).Trim
Next j
DataGridView1.Rows.Add(row)
Next i
End Sub
'Save
Private Sub บนทกงานToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles บนทกงานToolStripMenuItem.Click
Dim writer As New StreamWriter("C:\Memoryคาร์แคร์\SAVE\MemorySave.Txt")
For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
If j = DataGridView1.Columns.Count - 1 Then
writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "l")
Else
writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "l")
End If
Next j
writer.WriteLine("")
Next
writer.Close()
MessageBox.Show("บันทึกเรียบร้อย")
End Sub
End Class