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,027

HOME > .NET Framework > Forum > คลิกที่ datagridview แล้วให้ combobox เปลี่ยนตามอ่ะครับ รบกวนด้วยครับ



 

คลิกที่ datagridview แล้วให้ combobox เปลี่ยนตามอ่ะครับ รบกวนด้วยครับ

 



Topic : 045550

Guest




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 : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-07-16 13:38:40 By : GhostLocal View : 4038 Reply : 11
 

 

No. 1



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

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

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


แฮนเดิลอีเว็นต์ cellclick แล้วเอา index ของ dgv ไปหา datakey แล้วเอาไป query มา bind ให้ combo






Date : 2010-07-16 13:50:11 By : tungman
 


 

No. 2



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



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


มีตัวอย่างป่าวครับ ผมมือใหม่
Date : 2010-07-16 14:46:42 By : GhostLocal
 

 

No. 3



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

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

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


อันนี้ผมทำที่ 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
 


 

No. 4



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



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


ถ้าอย่างนี้พอไปต่อได้ครับ ขอบคุณมากมาย
Date : 2010-07-16 17:03:32 By : GhostLocal
 


 

No. 5



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



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


combobox ของผมไม่ได้ดึงมาจากฐานข้อมูลอ่ะครับผมใช้ combobox.item.add เอา อ่ะครับจะเขียนอย่างไงครับงง
Date : 2010-07-16 17:20:49 By : GhostLocal
 


 

No. 6



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

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

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


ไม่ได้ดึงจากฐานข้อมูลแต่ข้อมูลข้างใน combobox มันเหมือนกับฐานข้อมูลใช่มะครับ
Code (VB.NET)
    combobox.DataBindings.Add("text", ds, "table.field")

ยังไงมันก็ต้องเอา combobox ไป binding อยู่ดีครับไม่งั้นมันจะเปลี่ยนตาม datagridview ได้ยังไง
Date : 2010-07-16 18:27:11 By : noptalent
 


 

No. 7



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



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


ผมลองแก้ไขแล้วครับแต่ไม่ได้ นี่เป็นโค้ตทั้งหมดครับ ช่วยดูให้หน่อยครับ
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
 


 

No. 8



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



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


ช่วยผมด้วยครับ ทุกท่าน
Date : 2010-07-19 11:02:43 By : GhostLocal
 


 

No. 9

Guest


รบกวนด้วยครับ พี่ๆ
Date : 2010-07-19 13:27:18 By : GhostLocal
 


 

No. 10

Guest


อยากรู้เหมือนกันคับ
Date : 2012-02-16 10:55:01 By : nongmee
 


 

No. 11



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



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


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
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : คลิกที่ datagridview แล้วให้ combobox เปลี่ยนตามอ่ะครับ รบกวนด้วยครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่