 |
|
รบกวนด้วยค่ะ การส่งข้อมูลจาก Grid1 ไปยัง Grid2 ใน form เดียวกัน (VB2008) |
|
 |
|
|
 |
 |
|
สวัสดีค่ะ
คือมีปัญหาการเลือกข้อมูลใน Grid1 ไปยัง Grid2 ใน Form เดียวกัน ตอนนี้เลือกได้แค่ Row เดียวเท่านั้น
อยากเลือกข้อมูลได้ หลายรอบ โดยที่ข้อมูลที่เลือกไว้แล้วใน Grid2 ยังอยู่ด้วยอะค่ะ เพราะตอนนี้เลือกได้ทีละ Row
พอเลือก Row ใหม่ก็จะไปแทน Row เก่าทันที (คงเพราะใช้ CurrentRow ในการดึงค่าใน event CellMouseClick )
รบกวนทุกท่านด้วยนะคะ ว่าทำอย่างไรให้สามารถเลือกข้อมูลหลาย Row โดยที่เลือกไปแล้ว Row เก่าที่ส่งไปก่อนหน้ายังอยู่ สามารถเก็บค่า CurrentRow ได้หรือไม่ รบกวนจริง ๆ ค่ะ เป็นโปรเจคจบ หาทางแก้ปัญหาไม่ได้จริง ๆ ค่ะ
ใช้ VB2008 ค่ะ แล้วต้องการแบบคลิกส่งไปเลย ไม่เลือกไว้ก่อนแล้วส่งทีเดียวนะคะ
อันนี้คือรูป Gif เคลื่อนไหวที่ Capture ไว้ ค่ะ
http://img690.imageshack.us/img690/5684/22125532120532.gif
เพราะเห็นว่าให้ลงเฉพาะไฟล์ .jpg เท่านั้น ^^
แล้วก็ Code ส่วนนี้นะคะ ขอบพระคุณทุกท่านมาก ๆ ค่ะ
Code (VB.NET)
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
'-------------------------------------
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-01-23 17:14:08 |
By :
Kwang196 |
View :
4782 |
Reply :
11 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
grid1 ได้กำหนด keywordname ไว้ไหม จะได้ใช้อ้างอิงเวลาเอาค่าจาก CurrentRow
และลองศึกษาการ add new row ให้ datagridview ด้วย
|
 |
 |
 |
 |
Date :
2010-01-23 17:43:25 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เรื่อง Add new row ลองศึกษาแล้วค่ะ แต่เรียนตามตรง งงมาก ๆ พยายาม Coding ลองไปแล้ว ก็ไม่ได้อะคะ ตอนนี้เลยจับต้นชนปลายไม่ถูกแล้ว
ส่วนเรื่อง Keywordname ในการอ้าง CurrentRow คือ ตอนนี้ใช้ CurrentRow ไปเลยอ่ะคะ
หากมีวิธีให้การจัดการส่วน keyword รบกวนแนะนำทางสว่างด้วยนะคะ เพราะส่วนของการส่งข้อมูลนี้ เป็นส่วนสำคัญในการจัดการ Form อื่นด้วย ติดส่วนนี้แล้วยาวเลยไปต่อไม่ได้
รบกวนจริง ๆ ค่ะ ขอบคุณมาก ๆ นะคะ
|
 |
 |
 |
 |
Date :
2010-01-23 17:49:47 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช่แบบนี้หรือเปล่าครับ
ฟอร์มของผมมี DataGridView อยู่ 2 อัน ชื่อว่า DataGridView1 กับ DataGridView2
ถ้าผม Double Click ที่ Row ของ DataGridView1 Row นั้นจะ Add ไปที่ DataGridView2
ซึ่งข้อมูลที่ผมใช้นั้น Query จาก Sql Express เป็นตารางของชื่อเดือน

Code (VB.NET)
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
ให้ดูที่ Event DataGridView1_CellDoubleClick อาจจะได้คำตอบ
|
 |
 |
 |
 |
Date :
2010-01-23 22:33:59 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ tungman รบกวนหน่อยนะคะ และท่านอื่นๆ ด้วยค่ะ
คือพยายามคิดหลักการแล้วเพิ่งนึกออก แล้วนั่งอ่านตำรา เรื่อง array แล้วสรุปได้ว่า
คือ กวาง (ขอแทนชื่อตัวแล้วกันนะคะ ไม่อยากใช้คำว่าหนูหรือดิฉันอ่ะคะ)
ต้องจัดเก็บค่า CurrentRow ที่คลิกเลือกใน Grid1 ลงใน Array
เลือก 1 ครั้งก็เก็บไว้ใน array ชุดนั้น แล้วเวลาจะแสดงค่าใน Grid2 ก็ทำการดึงค่าเพื่อนำมา Select ข้อมูลใหม่ (หรือนำมาจาก dataset) ถูกต้องใช่ไหมค่ะ
ในทางตรงข้าม เมื่อคลิก Grid2 เพื่อทำการลบค่าออก ก็ให้เทียบค่าใน Array เพื่อนำค่าที่ต้องการลบออก และในขณะเดียวกัน Grid1 ก็ต้องนำค่าที่เพิ่งลบจาก Grid2 มาแสดงด้วย
ใช่อย่างนี้หรือไม่ค่ะ คิดวิธีออก แต่ algorithm คิดไม่ออกจริง ๆ ค่ะ ยิ่งกับ array ด้วยแล้ว มึนเลย ลองโค้ดไปแล้ว ก็ไม่ได้ค่ะ ติดตรงนี้มาพักใหญ่แล้ว
อีกอย่างคือ จะใช้การดึงค่าจาก dataset อย่างเดียวที่ได้จากการ select ในครั้งแรก หรือเมื่อมีการคลิกแต่ละครั้งต้องทำการ select จากฐานข้อมูลเสมอ
รบกวนแนะนำเป็นแนวทางการใช้ array เก็บตัวแปร CurrentRow และการดึงไปใช้ให้หน่อยได้ไหมค่ะ ในระหว่างนี้จะพยายามศึกษาด้วยตัวเองไปด้วย พร้อมรอคำแนะนำจากทุกท่านนะคะ
ขอบคุณมาก ๆค่ะ
|
 |
 |
 |
 |
Date :
2010-01-23 22:42:27 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่า มาแล้ว เพิ่งพิมพ์เสร็จพอดี เลยไม่ทราบว่าคุณ tungman มาตอบแล้ว เดี๋ยวขอลองศึกษาที่ให้มาสักครู่นะคะ ขอบคุณมาก ๆค่ะ
|
 |
 |
 |
 |
Date :
2010-01-23 22:43:29 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูที่ข้างบนนะ ผมเพิ่มการลบ row จาก datatable ให้แล้ว เผื่อถาม
|
 |
 |
 |
 |
Date :
2010-01-23 23:53:27 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ tungman ค่ะ กวางลองแล้วค่ะ ทั้ง Event DoubleClick และ MouseClick ก็ยังได้ผลเหมือนเดิมอ่ะคะ
คือเลือกได้แถวเดียว เลือกใหม่ก็ทับของเดิมตลอดอะคะ
นี่โค้ดที่กวางทำนะคะ รบกวนอีกครั้งนะคะ ขอบคุณมาก ๆ ค่ะ
Code (VB.NET)
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
|
 |
 |
 |
 |
Date :
2010-01-23 23:59:09 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมทำงานที่ DataTable ครับ ทำเสร็จค่อย Bind ลง DataGridView ลองทำตามตัวอย่างที่ให้จะเข้าใจเอง
|
 |
 |
 |
 |
Date :
2010-01-24 09:05:38 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พยายามทำความเข้าใจอย่างเต็มที่ค่า ^^" สงสัยเพราะยังไม่ได้นอน เลยเบลอ ๆ ยิ่งทำยิ่งงง
ถ้าติดปัญหาอีก ขอรบกวนด้วยนะคะ โปรเจคจบเหลือแค่ส่วนนี้ (กับอีก 3 ฟอร์มที่ทำงานแบบนี้เหมือนกัน)
ขอบคุณมาก ๆ นะคะ
|
 |
 |
 |
 |
Date :
2010-01-24 09:38:49 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็นี่มันฤดูปั่นโปรเจค 4x100 นี่นา
|
 |
 |
 |
 |
Date :
2010-01-24 09:49:02 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ tungman ขอบคุณมาก ๆ นะคะ ทำได้แล้ว
หลังจากได้นอนเต็มที่ 12 ชั่วโมงเต็ม สมองรับคำแนะนำเต็มสูบ ลองทำใหม่ยกเซต
ตอนนี้สำเร็จไปส่วนนึงแล้วค่า
หากมีปัญหาเพิ่มขึ้นมาอีก กวางขอรบกวนอีกครั้งนะคะ
ขอบคุณมาก ๆๆๆๆ ค่า
ป.ล. ของกวาง วิ่งระยะสั้น 50 เมตรค่ะ ไม่ใช่ 4X100 
|
 |
 |
 |
 |
Date :
2010-01-25 20:35:42 |
By :
Kwang196 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|