简体   繁体   English

根据 DataGridView 单元格值更新组合框选择?

[英]Update a ComboBox selection based on a DataGridView cell value?

I want my combo boxes to change based on the what is selected on my datagridview.我希望我的组合框根据在我的 datagridview 上选择的内容进行更改。

What I've tried:我试过的:

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

This code has no effect, but I do feel that I am heading in the right direction.这段代码没有效果,但我确实觉得我正朝着正确的方向前进。

On DataGridView2 properties go to SelectionMode and select FullRowSelected.在 DataGridView2 属性上,转到 SelectionMode 并选择 FullRowSelected。

Or add this line code on the Form1_Load(sender As Object, e As EventArgs)或者在 Form1_Load(sender As Object, e As EventArgs) 上添加这行代码

DataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect

If can also remove the ".SelectedRows IsNot Nothing AndAlso" because it will always have a value, even there is nothing selected it will return a Zero not a null.如果还可以删除“.SelectedRows IsNot Nothing AndAlso”,因为它始终具有值,即使没有选择任何内容,它也会返回零而不是空值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM