我有一个带有DatagridComboBoxColumn的数据网格,我希望Fire Event SelectionChanged当用户选择任何东西时从ComboBox,Do Some操作, 我怎么能这样做,任何建议, 谢谢
答案 0 :(得分:14)
您可以处理DataGridView的EditingControlShowing
事件,并将编辑控件强制转换为正在显示的ComboBox,然后将其SelectionChangeCommitted
事件连接起来。使用SelectionChangeCommitted
处理程序做你需要做的事。
请参阅我链接的MSDN文章中的示例代码以获取详细信息。
两个重要的注释:
尽管有MSDN文章的示例代码,但最好使用
ComboBox SelectionChangeCommitted
事件,正如here所讨论的那样
链接的MSDN文章的评论。
如果您有多个DatagridComboBoxColumn
DataGridView你可能想确定哪个是你的
EditingControlShowing
或ComboBox的SelectionChangeCommitted
事件。你可以通过检查你的DGV来做到这一点
CurrentCell.ColumnIndex
财产价值。
我重新编写了MSDN示例代码以显示我的意思:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
' Only for a DatagridComboBoxColumn at ColumnIndex 1.
If DataGridView1.CurrentCell.ColumnIndex = 1 Then
Dim combo As ComboBox = CType(e.Control, ComboBox)
If (combo IsNot Nothing) Then
' Remove an existing event-handler, if present, to avoid
' adding multiple handlers when the editing control is reused.
RemoveHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
' Add the event handler.
AddHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
End If
End If
End Sub
Private Sub ComboBox_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim combo As ComboBox = CType(sender, ComboBox)
Console.WriteLine("Row: {0}, Value: {1}", DataGridView1.CurrentCell.RowIndex, combo.SelectedItem)
End Sub