รบกวนพี่ ๆ ช่วยแนะนำหน่อยนะคะ จุดคับขันมาก ๆ ค่ะ เพราะว่า ต้องทำแบบเดียวกัน 5 Form
รูปที่ 1
รูปที่ 2 นะคะ
Code ค่ะ ส่วนของ DataTable
Code (VB.NET)
Function Get_ServiceData() As DataTable
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,StypeCode from services,service_price where ServCode = ServID and 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)
myCommand.Connection = myCon
myCon.Open()
Dim dataTable As DataTable = New DataTable
da.SelectCommand = New SqlCommand(st, myCon)
da.Fill(dataTable)
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
Return dataTable
End Function ' คัดกรองข้อมูลการบริการให้ตรงกับลักษณะของสัตว์เลี้ยง
ส่วนของการ Binding
Code (VB.NET)
Dim Grid_Service_Data As DataTable
Dim Grid_Treated_Data As DataTable
Private Sub Form_Treate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Choose_Vet()
Choose_Type()
Choose_System()
Cb_System.Enabled = False
lb_Date.Text = Date.Today
Grid_Service_Data = New DataTable
Grid_Treated_Data = New DataTable
'Grid การบริการ
Grid_Service_Data = Get_ServiceData() 'ข้อมูลใน Grid service มาจาก Function นี้ (กรองจากข้อมูลสัตว์เลี้ยง)
DataGrid_Service.DataSource = Grid_Service_Data
Set_GridSV()
DataGrid_Service.ReadOnly = True
DataGrid_Service.Enabled = False
'Grid การรักษา
Grid_Treated_Data = Grid_Service_Data.Clone()
DataGrid_Treated.DataSource = Grid_Treated_Data
Set_GridTreated()
End Sub ' Form Load
การเลือกข้อมูล
Code (VB.NET)
Private Sub DataGrid_Service_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGrid_Service.CellMouseDoubleClick
Grid_Treated_Data.ImportRow(Grid_Service_Data.Rows(e.RowIndex)) ' import ข้อมูลไปยัง datagird ของ Treated
DataGrid_Treated.DataSource = Grid_Treated_Data
Set_GridTreated()
Grid_Service_Data.Rows(e.RowIndex).Delete() ' ลบ row ที่เลือกไป
Grid_Service_Data.AcceptChanges()
DataGrid_Service.DataSource = Grid_Service_Data 'bind ข้อมูล
Set_GridSV()
'-------------------------------------
'คำนวณค่าบริการ()
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 'เลือกข้อมูลบริการ
ส่วนนี้เป็นส่วนเมื่อเราเลือกค่าใน ComboBox ค่ะ
Code (VB.NET)
Function Get_ServiceData_Type() As DataTable
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,StypeCode from services,service_price where ServCode = ServID and StypeCode = '" & Cb_Type.SelectedValue.ToString & "' and 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)
myCommand.Connection = myCon
myCon.Open()
Dim dataTable As DataTable = New DataTable
da.SelectCommand = New SqlCommand(st, myCon)
da.Fill(dataTable)
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
Return dataTable
End Function ' คัดกรองข้อมูลการบริการตามประเภท
Private Sub Cb_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cb_Type.SelectedIndexChanged
'binding service grid data.
Grid_Service_Data = Get_ServiceData()
DataGrid_Service.DataSource = Grid_Service_Data
Set_GridSV()
'clear grid2 data.
Grid_Treated_Data.Clear()
DataGrid_Treated.DataSource = Grid_Treated_Data
Set_GridTreated()
'clear service charges
SerAmount.Text = "0"
End Sub
กวางลองนำ Code ไปแล้ว ทั้งใน event SelectedIndexChanged กับ SelectedValueChanged ก็ไม่ได้ แจ้ง error "Object reference not set to an instance of an object."
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.IO
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
ComboBox1.DataSource = GetMonth()
ComboBox1.DisplayMember = "MonthName"
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))
'DataGridView2Data.DefaultView.Sort = "MonthID"
DataGridView2.DataSource = DataGridView2Data
DataGridView1Data.Rows(e.RowIndex).Delete()
DataGridView1Data.AcceptChanges()
DataGridView1.DataSource = DataGridView1Data
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))
'DataGridView1Data.DefaultView.Sort = "MonthID"
DataGridView1.DataSource = DataGridView1Data
DataGridView2Data.Rows(e.RowIndex).Delete()
DataGridView2Data.AcceptChanges()
DataGridView2.DataSource = DataGridView2Data
End Sub
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If e.ColumnIndex = Me.DataGridView1.Columns("MonthName").Index _
AndAlso (e.Value IsNot Nothing) Then
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ToolTipText = e.Value.ToString()
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'binding grid1 data.
DataGridView1Data = GetMonth()
DataGridView1.DataSource = DataGridView1Data
'clear grid2 data.
DataGridView2Data.Clear()
DataGridView2.DataSource = DataGridView2Data
End Sub
End Class