If DataGrid_Service.Rows.Count <> 0 Then
Dim da As SqlDataAdapter
da = New SqlDataAdapter 'ใช้ดึงข้อมูลจากฐานข้อมูลเก็บไว้ใน ds
Dim ds, ds2 As DataSet
ds = New DataSet
Dim st As String = "select ServID,ServName,ServPrice from Services where ServID ='" & Me.DataGrid_Service.CurrentRow.Cells(1).Value.ToString & "'"
Dim strcon As String = "Data Source=LT-DARKWANG\;Initial Catalog=DogandCat;Integrated Security=True"
Dim myCon As New SqlConnection(strcon)
Dim myCommand As New SqlCommand(st, myCon)
myCommand.Connection = myCon
myCon.Open()
da.SelectCommand = New SqlCommand(st, myCon)
Try
da.Fill(ds, "service")
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
'-------------------------------------
' Convert ประเภทสัตว์เลี้ยง
If (lb_TypePet.Text = "สุนัข") Then
lb_TypePet.Text = "D"
ElseIf (lb_TypePet.Text = "แมว") Then
lb_TypePet.Text = "C"
End If
' Convert เพศสัตว์เลี้ยง
If (lb_Sex.Text = "เมีย") Then
lb_Sex.Text = "F"
ElseIf (lb_Sex.Text = "ผู้") Then
lb_Sex.Text = "M"
End If
'-------------------------------------
Dim da2 As SqlDataAdapter
da2 = New SqlDataAdapter
ds2 = New DataSet
Dim st2 As String = "select ServID,ServName,ServPrice from Services where ServID <>'" & Me.DataGrid_Service.CurrentRow.Cells(1).Value.ToString & "' and ForType like '%" & lb_TypePet.Text & "%' and ForSex like '%" & lb_Sex.Text & "%'"
Dim myCon2 As New SqlConnection(strcon)
Dim myCommand2 As New SqlCommand(st2, myCon2)
myCommand2.Connection = myCon2
myCon2.Open()
da2.SelectCommand = New SqlCommand(st2, myCon2)
DataGrid_Treated.DataSource = ds.Tables("service")
Try
da2.Fill(ds2, "service2")
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
DataGrid_Service.DataSource = ds2.Tables("service2")
' For j As Integer = 0 To DataGrid_Treated.RowCount - 1
Dim cs As New DataGridViewCellStyle()
cs.Font = New Font("Browallia New", 14)
With DataGrid_Treated
.ColumnHeadersDefaultCellStyle = cs
.Columns("ServID").DisplayIndex = 0
.Columns(0).HeaderText = "รหัส"
.Columns(0).Width = 56%
.Columns(0).ReadOnly = True
.Columns("ServName").DisplayIndex = 1
.Columns(1).HeaderText = "ชื่อ"
.Columns(1).Width = 240%
.Columns(1).ReadOnly = True
.Columns("ServPrice").DisplayIndex = 2
.Columns(2).HeaderText = "ค่าบริการ(บาท)"
.Columns(2).Width = 128%
.Columns(2).ReadOnly = True
End With
With DataGrid_Service
.ColumnHeadersDefaultCellStyle = cs
.Columns("ServID").DisplayIndex = 1
.Columns(1).HeaderText = "รหัส"
.Columns(1).Width = 56%
.Columns(1).ReadOnly = True
.Columns("ServName").DisplayIndex = 2
.Columns(2).HeaderText = "ชื่อ"
.Columns(2).Width = 190%
.Columns(2).ReadOnly = True
.Columns("ServPrice").DisplayIndex = 3
.Columns(3).HeaderText = "ค่าบริการ(บาท)"
.Columns(3).Width = 100%
.Columns(3).ReadOnly = True
.Columns(3).Visible = False
End With
' End If
'-------------------------------------
' Convert ประเภทสัตว์เลี้ยง
If (lb_TypePet.Text = "D") Then
lb_TypePet.Text = "สุนัข"
ElseIf (lb_TypePet.Text = "C") Then
lb_TypePet.Text = "แมว"
End If
' Convert เพศสัตว์เลี้ยง
If (lb_Sex.Text = "F") Then
lb_Sex.Text = "เมีย"
ElseIf (lb_Sex.Text = "M") Then
lb_Sex.Text = "ผู้"
End If
'-------------------------------------
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim DataGridView1Data As DataTable
Dim DataGridView2Data As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1Data = New DataTable()
DataGridView2Data = New DataTable()
DataGridView1Data = GetMonth()
DataGridView1.DataSource = DataGridView1Data
DataGridView1.AllowUserToAddRows = False
DataGridView1.AllowUserToDeleteRows = False
DataGridView1.AllowUserToOrderColumns = False
DataGridView1.AllowUserToResizeColumns = False
DataGridView1.AllowUserToResizeRows = False
DataGridView1.ReadOnly = True
DataGridView2Data = DataGridView1Data.Clone() '<--สำคัญที่อันนี้แหละ
DataGridView2.DataSource = DataGridView2Data
DataGridView2.AllowUserToAddRows = False
DataGridView2.AllowUserToDeleteRows = False
DataGridView2.AllowUserToOrderColumns = False
DataGridView2.AllowUserToResizeColumns = False
DataGridView2.AllowUserToResizeRows = False
DataGridView2.ReadOnly = True
End Sub
Private Function GetMonth() As DataTable
Dim sqlConnectionString As String = "Data Source=TUNGMAN\SQLEXPRESS;Initial Catalog=SqlDatabase;Integrated Security=True"
Dim SqlConnection As SqlConnection = New SqlConnection(sqlConnectionString)
Dim sqlCommandString As String = "Select [MonthID], [MonthName] From [TableMonth]"
Dim sqlCommand As SqlCommand = New SqlCommand(sqlCommandString, SqlConnection)
Dim dataTable As DataTable = New DataTable()
Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
sqlDataAdapter.Fill(dataTable)
Return dataTable
End Function
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
DataGridView2Data.ImportRow(DataGridView1Data.Rows(e.RowIndex)) 'Import row from DataGridView1Data to DataGridView2Data.
DataGridView2Data.DefaultView.Sort = "MonthID" 'Sort DataGridView2Data by MonthID Field.
DataGridView2.DataSource = DataGridView2Data 'Bind data to DataGridView2.
DataGridView1Data.Rows(e.RowIndex).Delete() 'Delete selected row from DataGridView1Data.
DataGridView1Data.AcceptChanges() 'Confirm change data.
DataGridView1.DataSource = DataGridView1Data 'Bind data to DataGridView1.
End Sub
Private Sub DataGridView2_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellDoubleClick
DataGridView1Data.ImportRow(DataGridView2Data.Rows(e.RowIndex)) 'Import row from DataGridView2Data to DataGridView1Data.
DataGridView1Data.DefaultView.Sort = "MonthID" 'Sort DataGridView1Data by MonthID Field.
DataGridView1.DataSource = DataGridView1Data 'Bind data to DataGridView1.
DataGridView2Data.Rows(e.RowIndex).Delete() 'Delete selected row from DataGridView2Data.
DataGridView2Data.AcceptChanges() 'Confirm change data.
DataGridView2.DataSource = DataGridView2Data 'Bind data to DataGridView2.
End Sub
End Class
Sub filter_service()
If (lb_TypePet.Text = "สุนัข") Then
lb_TypePet.Text = "D"
ElseIf (lb_TypePet.Text = "แมว") Then
lb_TypePet.Text = "C"
End If
If (lb_Sex.Text = "เมีย") Then
lb_Sex.Text = "F"
ElseIf (lb_Sex.Text = "ผู้") Then
lb_Sex.Text = "M"
End If
Dim da As SqlDataAdapter = New SqlDataAdapter 'ใช้ดึงข้อมูลจากฐานข้อมูลเก็บไว้ใน ds
Dim st As String = "select ServID, ServName,ServPrice from services where ForType like '%" & lb_TypePet.Text & "%' and ForSex like '%" & lb_Sex.Text & "%'"
Dim strcon As String = "Data Source=LT-DARKWANG\;Initial Catalog=DogandCat;Integrated Security=True"
Dim myCon As New SqlConnection(strcon)
Dim myCommand As New SqlCommand(st, myCon)
Dim dt As DataTable = New DataTable
myCommand.Connection = myCon
myCon.Open()
da.SelectCommand = New SqlCommand(st, myCon)
Try
da.Fill(dt)
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
Dim dt2 As DataTable = New DataTable
DataGrid_Service.DataSource = dt
Set_GridSV()
dt2 = dt.Clone()
If (lb_TypePet.Text = "D") Then
lb_TypePet.Text = "สุนัข"
ElseIf (lb_TypePet.Text = "C") Then
lb_TypePet.Text = "แมว"
End If
If (lb_Sex.Text = "F") Then
lb_Sex.Text = "เมีย"
ElseIf (lb_Sex.Text = "M") Then
lb_Sex.Text = "ผู้"
End If
End Sub ' คัดกรองข้อมูลการบริการให้ตรงกับลักษณะของสัตว์เลี้ยง
Sub DataGrid_Service_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGrid_Service.CellMouseClick
Me.DataGrid_Service.ClearSelection()
' If DataGrid_Service.Rows.Count <> 0 Then
Dim da As SqlDataAdapter = New SqlDataAdapter 'ใช้ดึงข้อมูลจากฐานข้อมูลเก็บไว้ใน ds
Dim dt2 As DataTable = New DataTable
Dim st As String = "select ServID,ServName,ServPrice from Services where ServID ='" & Me.DataGrid_Service.CurrentRow.Cells(0).Value.ToString & "'"
Dim strcon As String = "Data Source=LT-DARKWANG\;Initial Catalog=DogandCat;Integrated Security=True"
Dim myCon As New SqlConnection(strcon)
Dim myCommand As New SqlCommand(st, myCon)
myCommand.Connection = myCon
myCon.Open()
da.SelectCommand = New SqlCommand(st, myCon)
Dim dataTable As DataTable = New DataTable
Try
da.Fill(dataTable)
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
End Try
'-------------------------------------
' Convert ประเภทสัตว์เลี้ยง
If (lb_TypePet.Text = "สุนัข") Then
lb_TypePet.Text = "D"
ElseIf (lb_TypePet.Text = "แมว") Then
lb_TypePet.Text = "C"
End If
' Convert เพศสัตว์เลี้ยง
If (lb_Sex.Text = "เมีย") Then
lb_Sex.Text = "F"
ElseIf (lb_Sex.Text = "ผู้") Then
lb_Sex.Text = "M"
End If
'-------------------------------------
dt2 = dataTable.Clone()
dt2.ImportRow(dataTable.Rows(0))
DataGrid_Treated.DataSource = dt2
With DataGrid_Treated
.Columns("ServID").DisplayIndex = 0
.Columns(0).HeaderText = "รหัส"
.Columns(0).Width = 56%
.Columns(0).ReadOnly = True
.Columns("ServName").DisplayIndex = 1
.Columns(1).HeaderText = "ชื่อ"
.Columns(1).Width = 240%
.Columns(1).ReadOnly = True
.Columns("ServPrice").DisplayIndex = 2
.Columns(2).HeaderText = "ค่าบริการ(บาท)"
.Columns(2).Width = 100%
.Columns(2).ReadOnly = True
End With
'-------------------------------------
' Convert ประเภทสัตว์เลี้ยง
If (lb_TypePet.Text = "D") Then
lb_TypePet.Text = "สุนัข"
ElseIf (lb_TypePet.Text = "C") Then
lb_TypePet.Text = "แมว"
End If
' Convert เพศสัตว์เลี้ยง
If (lb_Sex.Text = "F") Then
lb_Sex.Text = "เมีย"
ElseIf (lb_Sex.Text = "M") Then
lb_Sex.Text = "ผู้"
End If
'-------------------------------------
'คำนวณค่าบริการ
Dim total As Integer
For i As Integer = 0 To DataGrid_Treated.Rows.Count - 1
total = total + (CDbl(DataGrid_Treated.Rows(i).Cells(2).Value.ToString * 1))
Next
SerAmount.Text = total.ToString
End Sub