我在 VB.net(Visual Studio 2010)中有一个 DataGridView(称为 DataGridViewSecurity),它绑定到 DataSet(称为 DataSetSecurity)中的一个 DataTable(称为 DataTableSecurity)。我添加了一个基于 DataTable 中的整数字段(称为 nSecLevel)设置的非绑定列(称为 nSecurityComboBox)。设置组合框后,它不会在组合框中显示任何内容,但是当您选择组合框时,它的项目集合中的 5 个值会显示。
这是我用来向 DataTable 添加记录然后设置组合框的代码:
Sub Foo()
.
.
.
DataSetSecurity.Tables(0).Rows.Add(New Object() {sName, sID, sSec})
ComboCell_Select(nRow, 3, DataGridViewSecurity, sSecRecs.nSecLevel)
MessageBox.Show("Value for the combo set at " + DataGridViewSecurity.Rows(nRow).Cells(3).Value.ToString)
.
.
.
End Sub
Private Sub ComboCell_Select(ByVal dgvRow As Integer, _
ByVal dgvCol As Integer, _
ByRef DGV As DataGridView,
ByRef nComboBoxRow As Int16)
Try
Dim CBox As DataGridViewComboBoxCell = CType(DGV.Rows(dgvRow).Cells(dgvCol), DataGridViewComboBoxCell)
Dim CCol As DataGridViewComboBoxColumn = CType(DGV.Columns(dgvCol), DataGridViewComboBoxColumn)
CBox.Value = CCol.Items(nComboBoxRow)
DGV.UpdateCellValue(dgvCol, dgvRow)
'MessageBox.Show("New value in the combo box = " + CBox.Value.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Foo 中的 messagebox.show 显示了组合框的正确值,但没有显示任何内容。有人看到我做错了什么吗?
谢谢。
-NCGrimbo