Get the ID of selected item in Datagridview comboBox and show a form depend on Selectedvalue

Get the ID of selected ID in combobox and then show a form depending on the value of ID.

I have a DataGridView with a few columns in it and a combo box. I am using two data tables for this. dgv 1 is for the Data in the dgv and the other one is the item in the combo box. What I want to happen is when User chooses a status in the combo box, it will get the ID of the item, then If ID = 0 a form will show that the user needs to fill up.

Private Sub dgvPassed_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
    Dim comboCell As ComboBox = CType(e.Control, ComboBox)
    If (Not (comboCell) Is Nothing) Then
        AddHandler comboCell.SelectedIndexChanged, AddressOf Me.comboCell_SelectedIndexChanged
    End If

End Sub

Private Sub comboCell_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim cellText As String = dgvPassed.CurrentRow.Cells(7).Value.ToString
    'retrieve data from database using this cellText
End Sub

I tried using this and put breakpoint in it but after running and clicking in the datagridview combobox the breakpoint didnt even trigger.

1 answer

  • answered 2019-04-15 09:19 gjpio

    The combobox Q has been asked a few times, here for example;

    But here is some code (assuming two DGV columns Id & Status). You didn't say whether the Status was a value stored in your database or whether you were just using the column as sort of indicator (if indicator, the below code would need to reset the value back to "done" after editing form etc).

    Not that it matters, when you replace the datasource with your own you can sort the column behavior.

    Public Class Form1
        Public Class Sample
            Public Property Id As Integer
            Public Property Status As String
            Public Sub New(id As Integer, status As String)
                Me.Id = id
                Me.Status = status
            End Sub
        End Class
        Dim Source As New List(Of Sample)({New Sample(0, "Done"), New Sample(1, "Done"), New Sample(2, "Done"), New Sample(3, "Done")})
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DataGridView1.DataSource = Source
        End Sub
        Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
            If DataGridView1.IsCurrentCellDirty Then
            End If
        End Sub
        Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            If e.ColumnIndex < 0 OrElse e.RowIndex < 0 Then Exit Sub
            Dim ComboCell As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewComboBoxCell)
            If ComboCell?.Value?.ToString = "Edit" Then
                'Get ID (Hard-coded to 0 as an example)
                Dim IdCell As DataGridViewTextBoxCell = DirectCast(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewTextBoxCell)
                If IdCell?.Value = 0 Then
                    'Open your form here 
                    Using Openfd As New OpenFileDialog
                        If Openfd.ShowDialog() = DialogResult.OK Then
                            'Do stuff
                        End If
                    End Using
                End If
            End If
            '// redraw 
        End Sub
    End Class