Vb.net / DataGridView / ComboBox 列
我正在使用 vb.net 和 winforms。
我有一个带有绑定 DataGridView 的表单。在 DGV 上,我有 5 个带 ComboBox 的列。我正在使用 EditingControlShowing Even 来捕捉组合框选择。 (参见下面的代码)。 问题是:
当我单击带有组合框的单元格并进行选择,然后更新底层单元格(单元格=所选值),然后单击 DGV 的另一行时,它就会变得混乱。如果我更新单元格后,我会在数据源的相应行上执行并结束编辑,那么它似乎可以找到。
如何确定相应的数据源行以便我可以自动执行此操作?
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing
Try
Debug.Print("entered the EditingControlShowing")
Dim ColName As String = Me.DataGridView1.Columns(Me.DataGridView1.CurrentCell.ColumnIndex).Name
If ColName = "Col1" Then 'Or ColName = "Col2" Or ColName = "Col3" Or ColName = "Col4" Or ColName = "Col5" Then
'the column you want to cast
Dim cmb As ComboBox = TryCast(e.Control, ComboBox)
RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(Me.DataGridView1.CurrentCell.ColumnIndex).Value = CType(sender, ComboBox).SelectedItem
'
' HERE IF I PUT MyDataSet.Tables(0).Rows(?).EndEding it works - but how to konw what row?
'
UpdateAvgColumn(Me.DataGridView1.CurrentRow.Index)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I am using vb.net and winforms.
I have a Form with a Bound DataGridView. On the DGV I have 5 columns with ComboBox. I am using the EditingControlShowing Even to catch the ComboBox Selection. (see code bellow).
Here is the problem:
After I click on a Cell with a ComboBox and make a Selection and then update the underlying cell (cell = selected value) and then click on another Row of the DGV it goes haywire. If after I update the Cell I do and EndEdit on the corresponding row of the DataSource it seems to work find.
How can I determine whe corresponding Data Source row so that I can automate this?
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing
Try
Debug.Print("entered the EditingControlShowing")
Dim ColName As String = Me.DataGridView1.Columns(Me.DataGridView1.CurrentCell.ColumnIndex).Name
If ColName = "Col1" Then 'Or ColName = "Col2" Or ColName = "Col3" Or ColName = "Col4" Or ColName = "Col5" Then
'the column you want to cast
Dim cmb As ComboBox = TryCast(e.Control, ComboBox)
RemoveHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
AddHandler cmb.SelectedIndexChanged, AddressOf cmb_SelectedIndexChanged
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Try
Me.DataGridView1.Rows(Me.DataGridView1.CurrentRow.Index).Cells(Me.DataGridView1.CurrentCell.ColumnIndex).Value = CType(sender, ComboBox).SelectedItem
'
' HERE IF I PUT MyDataSet.Tables(0).Rows(?).EndEding it works - but how to konw what row?
'
UpdateAvgColumn(Me.DataGridView1.CurrentRow.Index)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
被解雇的事件正在重新进入。如果您当前正在处理一个事件,则必须小心不要处理其他事件。如果正在处理不同的事件,则必须在事件处理周围放置 IF 语句以跳过执行代码。
The events being fired are reentering. If you are currently processing an event, you have to be careful not to process other events. You have to put IF statements around the processing of the events to skip executing the code, if a different event is being processed.