根据DataGridView单元格值更新ComboBox选择?



我希望我的组合框根据我的datagridview上选择的内容进行更改。

我试过了:

Private Sub MyDataGridView_SelectionChanged2(sender As Object, e As EventArgs) Handles DataGridView2.SelectionChanged
With DataGridView2
If .SelectedRows IsNot Nothing AndAlso .SelectedRows.Count = 1 Then
Dim row As DataGridViewRow = .SelectedRows(0)

Form2.ComboBox1.Text = row.Cells(5).Value.ToString()
Form2.ComboBox2.Text = row.Cells(6).Value.ToString()
Form2.ComboBox3.Text = row.Cells(7).Value.ToString()
Form2.ComboBox4.Text = row.Cells(8).Value.ToString()
Form2.ComboBox5.Text = row.Cells(9).Value.ToString()
Form2.ComboBox6.Text = row.Cells(10).Value.ToString()
Form2.ComboBox7.Text = row.Cells(11).Value.ToString()
Form2.ComboBox8.Text = row.Cells(12).Value.ToString()
Form2.ComboBox9.Text = row.Cells(13).Value.ToString()

End If
End With
End Sub

这段代码没有效果,但我确实觉得我在朝着正确的方向前进。

这是我如何插入数据到组合框:

Dim sqlquery As String = "SELECT * FROM Ingredientes"
Using connection As SqlConnection = New SqlConnection("x")
connection.Open()
Using comm As SqlCommand = New SqlCommand(sqlquery, connection)
Dim rs As SqlDataReader = comm.ExecuteReader
Dim dt As DataTable = New DataTable
dt.Load(rs)

ComboBox1.BindingContext = New BindingContext
ComboBox1.DisplayMember = "Articulo"
ComboBox1.DataSource = dt
' etc

在DataGridView2属性上转到SelectionMode并选择FullRowSelected

或者在Form1_Load(sender As Object, e As EventArgs)上添加这行代码

DataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect

也可以去掉"。所选择的内容并非一无是处,也并非一无是处。因为它总是有一个值,即使没有选择,它也会返回一个0而不是null。

相关内容

  • 没有找到相关文章

最新更新