question

jumexmango-8448 avatar image
0 Votes"
jumexmango-8448 asked XingyuZhao-MSFT answered

Update a ComboBox selection based on a DataGridView cell value?

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




@XingyuZhao-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






dotnet-visual-basic
exmp1.gif (280.8 KiB)
exmp2.gif (282.8 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @jumexmango-8448 ,
How did you set your combobox data bound items? Please provide more information here, and it can will help us reproduce your problem.
I make a simple test on my side and I set the combobox data bound items in Fom2, and here's the result of my test:
143723-gif.gif
Code:

     Private dt As DataTable = New DataTable
     Private frm1 As New Form1
     Private list As New List(Of String)
     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         dt.Columns.Add("Column1")
         DataGridView1.DataSource = dt
         list.Add("NA")
         frm1.ComboBox1.DataSource = list
     End Sub 
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         dt.Rows.Add(TextBox1.Text)
         frm1.ComboBox1.DataSource = Nothing
         list.Insert(0, TextBox1.Text)
         frm1.ComboBox1.DataSource = list
     End Sub
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
         frm1.Show()
     End Sub

We are waiting for your update.



0 Votes 0 ·
gif.gif (58.2 KiB)

Here is how I set up my combobox databound items.
143838-capture.jpg



I have a table in SQL server that contain the list of items that appear on my combobox.

0 Votes 0 ·
capture.jpg (26.3 KiB)

1 Answer

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi @jumexmango-8448 ,
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.



case9.gif (102.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.