 |
|
คลิกที่ datagridview แล้วให้ combobox เปลี่ยนตามอ่ะครับ รบกวนด้วยครับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
'ปุ่มแก้ไขข้อมูล
Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
'ถ้าป้อนข้อมูลไม่ครบ
If (txtName.Text = "") Or (txtAddress.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลไม่ครบด้วยครับ", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
'สร้างตัวแปรเก็บชุดคำสั่ง SQL
Dim sqlUpdate As String = ""
Dim tmpRowsAffected As Long = 0 'ตัวแปรเก็บจำนวนเร็คคอร์ดที่กูกกระทำ
sqlUpdate = "UPDATE company SET"
sqlUpdate &= " name='" & txtName.Text & "','" & txtAddress.Text & "','" & cboShow.SelectedIndex & "'"
sqlUpdate &= " WHERE (name='" & txtName.Text & "')"
Try
'เชื่อมต่อกับฐานข้อมูล
ContoData()
Dim comUpdate As New MySqlCommand
With comUpdate
.CommandType = CommandType.Text
.CommandText = sqlUpdate
.Connection = Conn
tmpRowsAffected = .ExecuteNonQuery
End With
MessageBox.Show("แก้ไขข้อมูลเรียบร้อย", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
If tmpRowsAffected = 0 Then
MessageBox.Show("ชื่อบริษัทที่คุณเลือกไม่มีกรุณาลองใหม่", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtName.Focus()
txtName.SelectAll()
End If
Catch ex As MySqlException
MessageBox.Show("Cannot edit data,because " & ex.Message, "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtName.Focus()
txtName.SelectAll()
Exit Sub
Finally
Conn.Close()
End Try
ClearData()
ShowData()
txtName.Focus()
End Sub
'เหตุการณ์ DataGridview
Private Sub grdCompany_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdCompany.MouseUp
Dim UserSelectRow As Integer = 0 'ตัวแปรเก็บแถวที่ถูกคลิก
If IsFind = True Then
UserSelectRow = grdCompany.CurrentRow.Index
txtName.Text = CStr(grdCompany.CurrentRow.Cells(0).Value.ToString)
txtAddress.Text = CStr(grdCompany.CurrentRow.Cells(1).Value.ToString)
cboShow.SelectedIndex = CInt(grdCompany.CurrentRow.Cells(2).Value.ToString)
txtName.Focus()
End If
End Sub
ตามหัวข้อเลขครับ และก็โค้ตที่ผมทำอ่ะครับ
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-07-16 13:38:40 |
By :
GhostLocal |
View :
4120 |
Reply :
11 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แฮนเดิลอีเว็นต์ cellclick แล้วเอา index ของ dgv ไปหา datakey แล้วเอาไป query มา bind ให้ combo
|
 |
 |
 |
 |
Date :
2010-07-16 13:50:11 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีตัวอย่างป่าวครับ ผมมือใหม่
|
 |
 |
 |
 |
Date :
2010-07-16 14:46:42 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ผมทำที่ event RowHeaderMouseClick
Code (VB.NET)
Private Sub datagridview_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles datagridview.RowHeaderMouseClick
Dim IDCell As Integer
IDCell = CInt(datagridview.Rows(e.RowIndex).Index) 'ได้ตำแหน่ง cell ที่คลิก
Me.BindingContext(ds, "table1").Position = IDCell 'bindingcontext ของฟอร์ม จากตำแหน่งที่ได้
End Sub
ต่อไปก็ binding ให้กับ combobox
Code (VB.NET)
with combobox
.DataSource = ds.Tables("table2")
.DisplayMember = "field1"
.ValueMember = "field2" 'field1 กับ field2 ต้องมีความสัมพันธ์กันนะ เช่น field1 ชื่อเป็นประเภทสินค้า field2 เป็น id ประเภทสินค้า
.DataBindings.Add("SelectedValue", ds, "tablename1.field2")
end with
งงมั้ยครับ ผมทำเองยังงงเองเลย
ปล.ผลลัพธ์ อาจไม่ได้ผล เพราะเราจัดการ ds กับ dataAdapter ไม่เหมือนกันแน่
แต่วิธีทำประมาณนี้
|
 |
 |
 |
 |
Date :
2010-07-16 16:47:15 |
By :
noptalent |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าอย่างนี้พอไปต่อได้ครับ ขอบคุณมากมาย
|
 |
 |
 |
 |
Date :
2010-07-16 17:03:32 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
combobox ของผมไม่ได้ดึงมาจากฐานข้อมูลอ่ะครับผมใช้ combobox.item.add เอา อ่ะครับจะเขียนอย่างไงครับงง
|
 |
 |
 |
 |
Date :
2010-07-16 17:20:49 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้ดึงจากฐานข้อมูลแต่ข้อมูลข้างใน combobox มันเหมือนกับฐานข้อมูลใช่มะครับ
Code (VB.NET)
combobox.DataBindings.Add("text", ds, "table.field")
ยังไงมันก็ต้องเอา combobox ไป binding อยู่ดีครับไม่งั้นมันจะเปลี่ยนตาม datagridview ได้ยังไง
|
 |
 |
 |
 |
Date :
2010-07-16 18:27:11 |
By :
noptalent |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองแก้ไขแล้วครับแต่ไม่ได้ นี่เป็นโค้ตทั้งหมดครับ ช่วยดูให้หน่อยครับ
Code (VB.NET)
Private Sub FrmCompany_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor.Current = Cursors.WaitCursor
'เชื่อมต่อกับฐานข้อมูล
ContoData()
ClearData()
ShowData()
With cboShow
.Items.Add("ไม่แสดงชื่อบริษัท")
.Items.Add("แสดงชื่อบริษัท")
.SelectedIndex = 1
End With
cmdEdit.Enabled = False
cmdDelete.Enabled = False
txtName.Focus()
Cursor.Current = Cursors.Default
End Sub
Private Sub ClearData()
txtName.Text = ""
txtAddress.Text = ""
End Sub
Private Sub ShowData()
If IsFind = True Then 'ถ้าค่าสถานะของออบเจ็กต์ ds เท่ากับ true
ds.Tables("Company").Clear() 'ให้ล้างค่า
Else
IsFind = False 'ถ้าไม่มีก็กำหนดสถานะเป็น False
End If
'สร้างตัวแปรเก็บชุดคำสั่ง SQL
Dim sqlCompany As String = ""
sqlCompany = "SELECT * FROM company"
'เก็บชุดคำสั่ง SQL ไว้ในออบเจ็กต์ da
da = New MySqlDataAdapter(sqlCompany, Conn)
'ใสชุดข้อมูลลงในออบเจ็กต์ ds
da.Fill(ds, "Company")
'ถ้ามีอย่างน้อย 1 เร็คคอร์ด
If ds.Tables("Company").Rows.Count <> 0 Then
IsFind = True 'กำหนดให้ค่าสถานะ ds มีค่าเท่ากับ true
With grdCompany
.DataSource = ds.Tables("Company")
.Columns(0).HeaderText = "รหัสบริษัท"
.Columns(1).HeaderText = "ชื่อบริษัท"
.Columns(2).HeaderText = "ที่อยู่บริษัท"
.Columns(3).HeaderText = "สถานะแสดงชื่อบริษัท"
.Columns(0).Width = 80
.Columns(1).Width = 150
.Columns(2).Width = 250
.Columns(3).Width = 120
.Columns(0).ReadOnly = True
.Columns(1).ReadOnly = True
.Columns(2).ReadOnly = True
.Columns(3).ReadOnly = True
End With
Else
IsFind = False 'กำหนดให้ค่าสถานะ ds มีค่าเท่ากับ False
grdCompany.DataSource = Nothing 'ถ้าไม่มีข้อมูลให้ล้างข้อมูลใน DataGridView
End If
End Sub
'ปุ่มเพิ่มบริษัท
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If (txtName.Text = "") Or (txtAddress.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลให้ครบด้วยครับ.", "ผลการตรวจสอบ.", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtName.Focus()
Exit Sub
End If
'สร้างตัวแปรเก็บชุดคำสั่ง SQL
Dim sqlInsert As String = ""
sqlInsert = "INSERT INTO company (name,address,showinpurchase)"
sqlInsert &= " VALUES ('" & txtName.Text & "','" & txtAddress.Text & "','" & cboShow.SelectedIndex & "')"
Try 'ให้ทำ
'เชื่อมต่อกับฐานข้อมูล
ContoData()
'สร้างออบเจ็กต์ MySqlCommand ที่ชื่อว่า comInsert
Dim comInsert As New MySqlCommand
With comInsert
.CommandType = CommandType.Text
.CommandText = sqlInsert
.Connection = Conn
.ExecuteNonQuery()
End With
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้วครับ", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As MySqlException
MessageBox.Show("Cannot insert data,because " & ex.Message, "ผลการตรวจออบ", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtName.Focus()
txtName.SelectAll()
Finally
Conn.Close()
End Try
ClearData()
ShowData()
txtName.Focus()
End Sub
'ปุ่มแก้ไขข้อมูล
Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
'ถ้าป้อนข้อมูลไม่ครบ
If (txtName.Text = "") Or (txtAddress.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลไม่ครบด้วยครับ", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
'สร้างตัวแปรเก็บชุดคำสั่ง SQL
Dim sqlUpdate As String = ""
Dim tmpRowsAffected As Long = 0 'ตัวแปรเก็บจำนวนเร็คคอร์ดที่กูกกระทำ
sqlUpdate = "UPDATE company SET"
sqlUpdate &= " name='" & txtName.Text & "',address='" & txtAddress.Text & "',showinpurchase='" & cboShow.SelectedIndex & "'"
sqlUpdate &= " WHERE (name='" & txtName.Text & "')"
Try
'เชื่อมต่อกับฐานข้อมูล
ContoData()
Dim comUpdate As New MySqlCommand
With comUpdate
.CommandType = CommandType.Text
.CommandText = sqlUpdate
.Connection = Conn
tmpRowsAffected = .ExecuteNonQuery
End With
MessageBox.Show("แก้ไขข้อมูลเรียบร้อย", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
If tmpRowsAffected = 0 Then
MessageBox.Show("ชื่อบริษัทที่คุณเลือกไม่มีกรุณาลองใหม่", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtName.Focus()
txtName.SelectAll()
End If
Catch ex As MySqlException
MessageBox.Show("Cannot edit data,because " & ex.Message, "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtName.Focus()
txtName.SelectAll()
Exit Sub
Finally
Conn.Close()
End Try
ClearData()
ShowData()
txtName.Focus()
End Sub
'เหตุการณ์เมื่อคลิกที่ Cell ของ datagridview
Private Sub grdCompany_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdCompany.CellClick
Dim UserSelectRow As Integer = 0 'ตัวแปรเก็บแถวที่ถูกคลิก
If IsFind = True Then
UserSelectRow = CInt(grdCompany.Rows(e.RowIndex).Index)
txtName.Text = CStr(grdCompany.CurrentRow.Cells(0).Value.ToString)
txtAddress.Text = CStr(grdCompany.CurrentRow.Cells(1).Value.ToString)
Me.BindingContext(ds, "Company").Position = UserSelectRow
With cboShow
.DataSource = ds.Tables("Company")
.DataBindings.Add("SelectedValue", ds, "Company.showinpurchase")
End With
txtName.Focus()
End If
End Sub
|
 |
 |
 |
 |
Date :
2010-07-19 09:38:08 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยผมด้วยครับ ทุกท่าน
|
 |
 |
 |
 |
Date :
2010-07-19 11:02:43 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวนด้วยครับ พี่ๆ
|
 |
 |
 |
 |
Date :
2010-07-19 13:27:18 |
By :
GhostLocal |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากรู้เหมือนกันคับ
|
 |
 |
 |
 |
Date :
2012-02-16 10:55:01 |
By :
nongmee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
combobox ไม่ได้ดึงจากฐานข้อมูลแล้วข้อมูลมันเหมือนกะในฐานข้อมูลป่าว ถ้าเหมือน
Private Sub DataGridView1_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV1.CellMouseUp
If e.RowIndex = -1 Then
Exit Sub
End If
grdCompany.text = DataGridView1.Rows(e.RowIndex).Cells( 1 ตำแหน่งที่แสดงในดาต้ากิต).Value
End Sub
|
ประวัติการแก้ไข 2012-02-16 13:48:32
 |
 |
 |
 |
Date :
2012-02-16 13:47:57 |
By :
TheCom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|