Update a ComboBox selection based on a DataGridView cell value?

jumexmango 136 Reputation points
2021-10-25T16:16:13.477+00:00

I want my data bound combo boxes to change based on the what is selected on my datagridview. This is what I've tried.

  Private Sub MyDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged  
  
        With DataGridView1  
  
            If .SelectedRows IsNot Nothing AndAlso .SelectedRows.Count = 1 Then  
  
                Dim row As DataGridViewRow = .SelectedRows(0)  
  
                Form1.ComboBox1.Text = row.Cells(0).Value.ToString()  
  
  
            End If  
        End With  
    End Sub  

This code only works when the comboboxes don't use data bound items. Here are some examples.

Data bound comboboxes:

143475-exmp1.gif

None data bound comboboxes:(What I want)

143488-exmp2.gif

@Xingyu Zhao-MSFT

Edit (rest of code):

Public Class Form2  
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        DataGridView1.Rows.Add("repollo")  
  
    End Sub  
  
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
        Form1.Show()  
  
    End Sub  



Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
       
        Me.IngredientesTableAdapter.Fill(Me.Test1DataSet.Ingredientes)  
  
    End Sub  
End Class  
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-10-27T02:35:17.193+00:00

    Hi @jumexmango ,
    I set the same databound as yours and make a test on my side.
    Result of my test.
    143958-case9.gif
    Here's the code you can refer to:
    Form2:

        Private Sub Show_Click(sender As Object, e As EventArgs) Handles Show.Click  
            With DataGridView1  
                If .SelectedRows IsNot Nothing AndAlso .SelectedRows.Count = 1 Then  
                    Dim row As DataGridViewRow = .SelectedRows(0)  
                    Dim st As String = row.Cells(0).Value.ToString()  
      
                    Using Form1 As New Form1  
                        Form1.dgvdata = st  
                        Form1.ShowDialog()  
                    End Using  
                End If  
            End With  
        End Sub  
    

    Form1:

        Public dgvdata As String  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Me.IngredientesTableAdapter.Fill(Me.YourDataSet.Ingredientes)  
      
            Dim dr As DataRow = Me.YourDataSet.Ingredientes.NewRow()  
            dr("Articulo") = dgvdata  
            Me.YourDataSet.Table1.Rows.InsertAt(dr, 0)  
      
            ComboBox1.DataSource = Me.YourDataSet.Ingredientes  
            ComboBox1.DisplayMember = "Articulo"  
            ComboBox1.ValueMember = "Articulo"  
      
        End Sub  
    

    Hope the code above could be helpful.

    Best Regards.
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful