Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to get the selected value of a ComboBox in a DataGridView.
I'm getting the text only when I click combobox column first.
The text is not showing if I click another column first, combobox coloumn second.

Please help me with this.

What I have tried:

VB
 Private Sub Combo_SelectedIndexChanged
(sender As Object, e As EventArgs)

        Dim selectedValue = DirectCast(sender, ComboBox).SelectedItem

        If DataGridView3.CurrentCell.ColumnIndex > 0 Then
            DataGridView3.CurrentCell.Value = selectedValue
        End If

    End Sub

    Private Sub DataGridView3_CellClick(sender As Object, _
    e As DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        combo.Items.Clear()

        For i = 0 To 10
            combo.Items.Add(PLAYERDG.Rows(i).Cells(1).Value)
        Next

        Me.DataGridView3.Controls.Add(combo)
        combo.Location = Me.DataGridView3.GetCellDisplayRectangle_
        (1, DataGridView3.CurrentCell.RowIndex, True).Location
        combo.Size = Me.DataGridView3.GetCellDisplayRectangle_
        (1, DataGridView3.CurrentCell.RowIndex, True).Size

        If (combo IsNot Nothing) Then

            AddHandler combo.SelectedIndexChanged, AddressOf Combo_SelectedIndexChanged

        End If

    End Sub
Posted
Updated 2-Oct-23 5:03am
v2

1 solution

This is a common task. Here is a google search with many examples: winform datagrid combobox selecteditem[^]

Or you can ask ChatGpt this question:
I need to get the selected value of a ComboBox in a winform  DataGridView using VB.Net?

ChatGpt reply starts with:
To get the selected value of a ComboBox in a WinForms DataGridView using VB.NET, you can handle the CellValueChanged event of the DataGridView and check if the cell that changed is the ComboBox cell. Here's a step-by-step guide on how to do this:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900