'* 展开Combobox列时 Private Sub Me_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles Me.EditingControlShowing If InStr(e.Control.ToString, "DataGridViewComboBoxEditingControl", CompareMethod.Text) > 0 Then Dim DgComboBox As ComboBox = CType(e.Control, ComboBox) RemoveHandler DgComboBox.SelectionChangeCommitted, AddressOf ComboBox_SelectedIndexChanged '// 防止多次绑定事件,多次绑定会让一个事件被多次执行 AddHandler DgComboBox.SelectionChangeCommitted, AddressOf ComboBox_SelectedIndexChanged End If End Sub ' '* 选择列中的Combobox时写入值: Private StopWrtieCurrentChangeComboboxIndex As Boolean = False '// 用来阻止,将DataTable的值写入数据源时,同时又触发ComboBox_SelectedIndexChanged() Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) If StopWrtieCurrentChangeComboboxIndex = True Then '// Return End If Dim DgComboBox As ComboBox = CType(sender, ComboBox) If DgComboBox.DataSource Is Nothing Then '// 1.绑定到数组:不需设置 'Me.CurrentCell.Value = Trim(DgComboBox.SelectedItem) ElseIf InStr(DgComboBox.DataSource.GetType.ToString, "DataTable", CompareMethod.Text) > 0 Then '// 2.绑定到System.Data.DataTable:根据映射数组设置来填写数据 StopWrtieCurrentChangeComboboxIndex = True If InStr(Me.DataSource.GetType.ToString, "BindingSource", CompareMethod.Text) > 0 Then Dim GridBindSource As BindingSource = Me.DataSource GridBindSource.Current.item("Field1") = DgComboBox.Items(DgComboBox.SelectedIndex).item("FieldValue") GridBindSource.EndEdit() End If StopWrtieCurrentChangeComboboxIndex = False End If End Sub
用vb2005编程,datagrideview实现下拉框,绑定数据表,表中有编号和名称,当我在下拉框中选择编号后,在其他列马上显示编号相对应的名称
最新推荐文章于 2022-08-25 00:05:06 发布