我有一个DataGridView,它有一个ComboBoxColumn。此列绑定到DataTable:
With dtYesOrNo
.Rows.Clear()
.Columns.Clear()
.Columns.Add("Description")
.Columns.Add("Code")
Dim statusRow = dtYesOrNo.NewRow
statusRow("Description") = ""
statusRow("Code") = 101
dtYesOrNo.Rows.Add(statusRow)
statusRow = dtYesOrNo.NewRow
statusRow("Description") = "Yes"
statusRow("Code") = 102
dtYesOrNo.Rows.Add(statusRow)
statusRow = dtYesOrNo.NewRow
statusRow("Description") = "No"
statusRow("Code") = 103
dtYesOrNo.Rows.Add(statusRow)
End With
然后我将DisplayMember设置为“Description”,将ValueMember设置为“Code”。
Dim colUECStatus As New DataGridViewComboBoxColumn
With colUECStatus
.DataSource = dtYesOrNo
.DataPropertyName = "StatusChk"
.Name = "StatusChk"
.DisplayMember = "Description"
.ValueMember = "Code"
'Combobox Formatting
.HeaderText = "Status"
.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
.FlatStyle = FlatStyle.Flat
.DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 192)
UECgrid.Columns.Insert(0, colUECStatus)
End With
但我的问题是,当我实际加载表单时,它给了我一个例外,因为我的combox中没有有效值。例如,如果选择是“”,则组合框将显示101,而不是“”。 这里是我加载网格的代码(注意:请忽略网格名称 - UECgrid和dgvUEC1是相同的网格):
Dim row = 0
For Each checklist In USEFileChecklistUSChecklist.GetGeneralChecklist
objEFile.dgvUEC1.Rows(row).Cells("StatusChk").Value = checklist.statusCode
row += 1
Next