单击datagridview中的数据后,数据将不会显示在COMBOBOX中。 VB NET

时间:2015-01-25 03:45:15

标签: vb.net datagridview combobox

我的表单包含DataGridView和三个ComboBox控件。在datagrid中,有三列。现在,当我单击datagridview(CellClick事件)时,我单击的数据应该显示在组合框上。但是当我这样做时,其他数据都没有显示出来。只有一个能够在datagridview中显示单击的单元格。 这是我的代码。

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If e.RowIndex >= 0 Then
        row = Me.DataGridView1.Rows(e.RowIndex)
        ComboBox1.Text = row.Cells("GRADE LEVEL").Value
        cbSubject.Text = row.Cells("SUBJECT").Value
        cbTeacher.Text = row.Cells("TEACHER").Value
    End If
End Sub 

是否还有其他代码可以替换它。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这是你想要的吗? 我不能理解这个问题,如果我误解了,请原谅我:)

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.RowIndex >= 0 Then
            ComboBox1.DataSource = DataGridView1.DataSource
            ComboBox1.DisplayMember = "GRADE LEVEL"  'Column name as string here
            cbSubject.DataSource = DataGridView1.DataSource
            cbSubject.DisplayMember = "SUBJECT"
            cbTeacher.DataSource = DataGridView1.DataSource
            cbTeacher.DisplayMember = "TEACHER"
        Else 'clear the combo box
            ComboBox1.DataSource = Nothing 
            cbSubject.DataSource = Nothing
            cbTeacher.DataSource = Nothing
        End If
    End Sub

并且您可能希望添加此代码以在单击时从datagridview中选择整行

DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

你可以把它放在表格加载或新的