Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > รบกวนค่ะ ส่งข้อมูลจาก Grid1 ไปยัง Grid2 เพิ่มการเลือกข้อมูลจาก ComboBox (VB2008)



 

[.NET] รบกวนค่ะ ส่งข้อมูลจาก Grid1 ไปยัง Grid2 เพิ่มการเลือกข้อมูลจาก ComboBox (VB2008)

 



Topic : 038405



โพสกระทู้ ( 46 )
บทความ ( 0 )



สถานะออฟไลน์




จริง ๆ ทำเสร็จหมดแล้ว (ต้องขอบคุณคุณ Tungman มาก ๆ ค่ะ) แต่อาจารย์ต้องการให้เพิ่มการเลือกข้อมูลตามลำดับความสัมพันธ์ เพื่อความรวดเร็วและสะดวกของผู้ใช้งาน จึงต้องต้องปรับเปลี่ยนการเลือกข้อมูล โดยเพิ่ม ComboBox เข้ามาค่ะ แต่กวางทำแล้วเกิดปัญหา

โดยเมื่อกวางเลือกข้อมูลจาก Grid1 ไปยัง Grid2 เรียบร้อยแล้ว และต้องการเลือกข้อมูลเพิ่ม และใช้ ComboBox ในการกรอกข้อมูลในตรงกับความต้องการเพื่อความรวดเร็ว ปรากฎว่า เมื่อเลือกค่าใน ComboBox แล้ว ข้อมูลที่เลือกไว้ใน Grid 2 ก็หายไปค่ะ

รบกวนพี่ ๆ ช่วยแนะนำหน่อยนะคะ จุดคับขันมาก ๆ ค่ะ เพราะว่า ต้องทำแบบเดียวกัน 5 Form

รูปที่ 1

เริ่มการเลือกข้อมูล

รูปที่ 2 นะคะ
ปัญหาที่เกิดขึ้น

Code ค่ะ
ส่วนของ DataTable

Code (VB.NET)
01.Function Get_ServiceData() As DataTable
02.        If (lb_TypePet.Text = "สุนัข") Then
03.            lb_TypePet.Text = "D"
04.        ElseIf (lb_TypePet.Text = "แมว") Then
05.            lb_TypePet.Text = "C"
06.        End If
07.        If (lb_Sex.Text = "เมีย") Then
08.            lb_Sex.Text = "F"
09.        ElseIf (lb_Sex.Text = "ผู้") Then
10.            lb_Sex.Text = "M"
11.        End If
12.        Dim da As SqlDataAdapter = New SqlDataAdapter 'ใช้ดึงข้อมูลจากฐานข้อมูลเก็บไว้ใน ds
13.        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 & "%'"
14.        Dim strcon As String = "Data Source=LT-DARKWANG\;Initial Catalog=DogandCat;Integrated Security=True"
15.        Dim myCon As New SqlConnection(strcon)
16.        Dim myCommand As New SqlCommand(st, myCon)
17.        myCommand.Connection = myCon
18.        myCon.Open()
19.        Dim dataTable As DataTable = New DataTable
20.        da.SelectCommand = New SqlCommand(st, myCon)
21.        da.Fill(dataTable)
22.        If (lb_TypePet.Text = "D") Then
23.            lb_TypePet.Text = "สุนัข"
24.        ElseIf (lb_TypePet.Text = "C") Then
25.            lb_TypePet.Text = "แมว"
26.        End If
27.        If (lb_Sex.Text = "F") Then
28.            lb_Sex.Text = "เมีย"
29.        ElseIf (lb_Sex.Text = "M") Then
30.            lb_Sex.Text = "ผู้"
31.        End If
32.        Return dataTable
33.    End Function ' คัดกรองข้อมูลการบริการให้ตรงกับลักษณะของสัตว์เลี้ยง


ส่วนของการ Binding

Code (VB.NET)
01.Dim Grid_Service_Data As DataTable
02.   Dim Grid_Treated_Data As DataTable
03. 
04.Private Sub Form_Treate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
05.       Choose_Vet()
06.       Choose_Type()
07.       Choose_System()
08.       Cb_System.Enabled = False
09. 
10. 
11.       lb_Date.Text = Date.Today
12. 
13.       Grid_Service_Data = New DataTable
14.       Grid_Treated_Data = New DataTable
15. 
16.       'Grid การบริการ
17.       Grid_Service_Data = Get_ServiceData() 'ข้อมูลใน Grid service มาจาก Function นี้ (กรองจากข้อมูลสัตว์เลี้ยง)
18.       DataGrid_Service.DataSource = Grid_Service_Data
19.       Set_GridSV()
20.       DataGrid_Service.ReadOnly = True
21.       DataGrid_Service.Enabled = False
22.       'Grid การรักษา
23.       Grid_Treated_Data = Grid_Service_Data.Clone()
24.       DataGrid_Treated.DataSource = Grid_Treated_Data
25.       Set_GridTreated()
26. 
27.   End Sub ' Form Load


การเลือกข้อมูล

Code (VB.NET)
01.Private Sub DataGrid_Service_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGrid_Service.CellMouseDoubleClick
02. 
03.       Grid_Treated_Data.ImportRow(Grid_Service_Data.Rows(e.RowIndex)) ' import ข้อมูลไปยัง datagird ของ Treated
04.       DataGrid_Treated.DataSource = Grid_Treated_Data
05.       Set_GridTreated()
06.       Grid_Service_Data.Rows(e.RowIndex).Delete() ' ลบ row ที่เลือกไป
07.       Grid_Service_Data.AcceptChanges()
08.       DataGrid_Service.DataSource = Grid_Service_Data 'bind ข้อมูล
09.       Set_GridSV()
10. 
11.       '-------------------------------------
12.       'คำนวณค่าบริการ()
13.       Dim total As Integer
14.       For i As Integer = 0 To DataGrid_Treated.Rows.Count - 1
15.           total = total + (CDbl(DataGrid_Treated.Rows(i).Cells(2).Value.ToString * 1))
16.       Next
17.       SerAmount.Text = total.ToString
18. 
19.   End Sub 'เลือกข้อมูลบริการ


ส่วนนี้เป็นส่วนเมื่อเราเลือกค่าใน ComboBox ค่ะ

Code (VB.NET)
01.Function Get_ServiceData_Type() As DataTable
02.        If (lb_TypePet.Text = "สุนัข") Then
03.            lb_TypePet.Text = "D"
04.        ElseIf (lb_TypePet.Text = "แมว") Then
05.            lb_TypePet.Text = "C"
06.        End If
07.        If (lb_Sex.Text = "เมีย") Then
08.            lb_Sex.Text = "F"
09.        ElseIf (lb_Sex.Text = "ผู้") Then
10.            lb_Sex.Text = "M"
11.        End If
12.        Dim da As SqlDataAdapter = New SqlDataAdapter 'ใช้ดึงข้อมูลจากฐานข้อมูลเก็บไว้ใน ds
13.        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 & "%'"
14.        Dim strcon As String = "Data Source=LT-DARKWANG\;Initial Catalog=DogandCat;Integrated Security=True"
15.        Dim myCon As New SqlConnection(strcon)
16.        Dim myCommand As New SqlCommand(st, myCon)
17.        myCommand.Connection = myCon
18.        myCon.Open()
19.        Dim dataTable As DataTable = New DataTable
20.        da.SelectCommand = New SqlCommand(st, myCon)
21.        da.Fill(dataTable)
22.        If (lb_TypePet.Text = "D") Then
23.            lb_TypePet.Text = "สุนัข"
24.        ElseIf (lb_TypePet.Text = "C") Then
25.            lb_TypePet.Text = "แมว"
26.        End If
27.        If (lb_Sex.Text = "F") Then
28.            lb_Sex.Text = "เมีย"
29.        ElseIf (lb_Sex.Text = "M") Then
30.            lb_Sex.Text = "ผู้"
31.        End If
32.        Return dataTable
33.    End Function ' คัดกรองข้อมูลการบริการตามประเภท


รบกวนทุกท่านด้วยนะคะ ใกล้ Demo โปรเจคแล้ว..งานดันเข้า..เล่นเอาเครียดมาก ๆ เลยค่ะ



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-02-05 19:46:26 By : Kwang196 View : 3391 Reply : 5
 

 

No. 1

Guest


Code (VB.NET) For ComboBox
01.Private Sub Cb_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cb_Type.SelectedIndexChanged
02.    'binding service grid data.
03.    Grid_Service_Data = Get_ServiceData()
04.    DataGrid_Service.DataSource = Grid_Service_Data
05.    Set_GridSV()
06. 
07.    'clear grid2 data.
08.    Grid_Treated_Data.Clear()
09.    DataGrid_Treated.DataSource = Grid_Treated_Data
10.    Set_GridTreated()
11. 
12.    'clear service charges
13.    SerAmount.Text = "0"
14.End Sub







Date : 2010-02-05 21:14:30 By : tungman
 


 

No. 2



โพสกระทู้ ( 46 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมาก ๆ ค่ะ เดี๋ยวกวางลองดูนะคะ แล้วจะกลับมาแจ้งผลค่า ^^
Date : 2010-02-05 21:29:07 By : Kwang196
 

 

No. 3



โพสกระทู้ ( 46 )
บทความ ( 0 )



สถานะออฟไลน์


กวางลองนำ Code ไปแล้ว ทั้งใน event SelectedIndexChanged กับ SelectedValueChanged ก็ไม่ได้ แจ้ง error "Object reference not set to an instance of an object."

แต่พอลองกับ SelectionChangeCommitted ไม่ error ค่ะ แต่การทำงานเหมือนเดิม คือ เลือกข้อมูลไว้แล้ว พอจะเลือกเพิ่ม..แล้วเลือก ComboBox ก่อน ข้อมูลที่เลือกไว้แล้วหายไปทั้ง Grid2 เลยค่ะ แล้วก็ เมื่อเลือกค่าใน ComboBox ก็ไม่มีการกรอง ค่าตามประเภทด้วยค่ะ

รบกวนอีกครั้งนะคะ ตอนนี้ก็กำลังพยายามอยู่ค่ะ ทำแบบ Add Row ก็มีปัญหาอีกเรื่อง รบกวนด้วยจริง ๆ ค่า
Date : 2010-02-05 21:39:36 By : Kwang196
 


 

No. 4



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


1

Code (VB.NET)
01.Imports System.Data
02.Imports System.Data.SqlClient
03.Imports System.Data.OleDb
04.Imports System.IO
05. 
06.Public Class Form1
07.    Dim DataGridView1Data As DataTable
08.    Dim DataGridView2Data As DataTable
09. 
10.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
11.        DataGridView1Data = New DataTable()
12.        DataGridView2Data = New DataTable()
13. 
14.        DataGridView1Data = GetMonth()
15.        DataGridView1.DataSource = DataGridView1Data
16.        DataGridView1.AllowUserToAddRows = False
17.        DataGridView1.AllowUserToDeleteRows = False
18.        DataGridView1.AllowUserToOrderColumns = False
19.        DataGridView1.AllowUserToResizeColumns = False
20.        DataGridView1.AllowUserToResizeRows = False
21.        DataGridView1.ReadOnly = True
22. 
23.        DataGridView2Data = DataGridView1Data.Clone()
24.        DataGridView2.DataSource = DataGridView2Data
25.        DataGridView2.AllowUserToAddRows = False
26.        DataGridView2.AllowUserToDeleteRows = False
27.        DataGridView2.AllowUserToOrderColumns = False
28.        DataGridView2.AllowUserToResizeColumns = False
29.        DataGridView2.AllowUserToResizeRows = False
30.        DataGridView2.ReadOnly = True
31. 
32.        ComboBox1.DataSource = GetMonth()
33.        ComboBox1.DisplayMember = "MonthName"
34.    End Sub
35. 
36.    Private Function GetMonth() As DataTable
37.        Dim sqlConnectionString As String = "Data Source=TUNGMAN\SQLEXPRESS;Initial Catalog=SqlDatabase;Integrated Security=True"
38.        Dim SqlConnection As SqlConnection = New SqlConnection(sqlConnectionString)
39. 
40.        Dim sqlCommandString As String = "Select [MonthID], [MonthName] From [TableMonth]"
41.        Dim sqlCommand As SqlCommand = New SqlCommand(sqlCommandString, SqlConnection)
42. 
43.        Dim dataTable As DataTable = New DataTable()
44.        Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
45.        sqlDataAdapter.Fill(dataTable)
46. 
47.        Return dataTable
48.    End Function
49. 
50.    Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
51.        DataGridView2Data.ImportRow(DataGridView1Data.Rows(e.RowIndex))
52.        'DataGridView2Data.DefaultView.Sort = "MonthID"
53.        DataGridView2.DataSource = DataGridView2Data
54. 
55.        DataGridView1Data.Rows(e.RowIndex).Delete()
56.        DataGridView1Data.AcceptChanges()
57.        DataGridView1.DataSource = DataGridView1Data
58.    End Sub
59. 
60.    Private Sub DataGridView2_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellDoubleClick
61.        DataGridView1Data.ImportRow(DataGridView2Data.Rows(e.RowIndex))
62.        'DataGridView1Data.DefaultView.Sort = "MonthID"
63.        DataGridView1.DataSource = DataGridView1Data
64. 
65.        DataGridView2Data.Rows(e.RowIndex).Delete()
66.        DataGridView2Data.AcceptChanges()
67.        DataGridView2.DataSource = DataGridView2Data
68.    End Sub
69. 
70.    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
71.        If e.ColumnIndex = Me.DataGridView1.Columns("MonthName").Index _
72.            AndAlso (e.Value IsNot Nothing) Then
73. 
74.            Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ToolTipText = e.Value.ToString()
75.        End If
76.    End Sub
77. 
78.    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
79.        'binding grid1 data.
80.        DataGridView1Data = GetMonth()
81.        DataGridView1.DataSource = DataGridView1Data
82. 
83.        'clear grid2 data.
84.        DataGridView2Data.Clear()
85.        DataGridView2.DataSource = DataGridView2Data
86.    End Sub
87.End Class

Date : 2010-02-05 21:52:16 By : tungman
 


 

No. 5



โพสกระทู้ ( 46 )
บทความ ( 0 )



สถานะออฟไลน์


หลังจากลองดัดแปลง ปรับ ตัด ก็ได้แล้วค่ะ

Code ดังนี้ค่ะ

Code (VB.NET)
01.Private Sub Cb_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cb_Type.SelectedIndexChanged
02.    Grid_Service_Data = Get_ServiceData_Type()
03.    DataGrid_Service.DataSource = Grid_Service_Data
04.    Set_GridSV()
05. 
06.    'clear grid2 data.
07.    ' Grid_Treated_Data.Clear()
08.    'DataGrid_Treated.DataSource = Grid_Treated_Data
09.    'Set_GridTreated()
10. 
11.    'clear service charges
12.    'SerAmount.Text = "0"
13. 
14.End Sub


แต่ยังติดปัญหาอะคะ คือว่า ข้อมูลใน Grid1 และ 2 ซ้ำกัน ตามรูปเลยค่ะ รบกวนอีกครั้งนะคะ

ค่าซ้ำกัน

เพิ่มเติมนะคะ ค่าจะซ้ำ เมื่อกวางกลับมาเลือก ComboBox เดิมซ้ำอีกครั้ง หลังจากที่เลือกค่าไปยัง Grid 2 เรียบร้อยแล้วอ่ะคะ
Date : 2010-02-05 21:54:25 By : Kwang196
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : รบกวนค่ะ ส่งข้อมูลจาก Grid1 ไปยัง Grid2 เพิ่มการเลือกข้อมูลจาก ComboBox (VB2008)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่