DataGridView单元格动态绑定选择项
首先,需要在窗体初次导入时,设置一下所有可能的选择项
Me.coolMainProcessColumn.DataSource = EnumProvider._global.getCoolMainProcess()或Me.xFFieldNameDataGridViewTextBoxColumn.DataSource = pFieldList '一定要先绑定DataSource Me.xFFieldNameDataGridViewTextBoxColumn.DisplayMember = ValueObject.displayMemeber Me.xFFieldNameDataGridViewTextBoxColumn.ValueMember = ValueObject.valueMember
然后在CellBeginEdit事件中加入动态绑定语法
Dim cmbCell As DataGridViewComboBoxCell
cmbCell = CType(dgHot.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewComboBoxCell)
cmbCell.DataSource = EnumProvider._global.getHotSubProcessByMain(mainProcess)
最后,对选择项最好要在RowEnter事件中设置选择项的默认值,以减少出错的机会
'几个选择项,自动设为一个默认值,否则退出时会出错。 If (Me.xDataGridView1.Rows(e.RowIndex).IsNewRow) Then If (Me.xBindingSourceObject.Current IsNot Nothing) Then '从绑定的数据源取当前值,作为默认值 Dim pRowView As DataRowView = CType(Me.xBindingSourceObject.Current, DataRowView) Dim pRow As DSTankData.TankDataRow = CType(pRowView.Row, DSTankData.TankDataRow) Me.xDataGridView1.Rows(e.RowIndex).Cells(Me.xFObjectNameDataGridViewTextBoxColumn.Index).Value = pRow.TankName End If Me.xDataGridView1.Rows(e.RowIndex).Cells(Me.xFFieldNameDataGridViewTextBoxColumn.Index).Value = _defaultFieldValue end if