我的表单包含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
是否还有其他代码可以替换它。任何帮助表示赞赏。
答案 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
你可以把它放在表格加载或新的
中