Vb.net / DataGridView / ComboBox 列

发布于 2024-10-20 20:11:24 字数 1654 浏览 9 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

会傲 2024-10-27 20:11:24

被解雇的事件正在重新进入。如果您当前正在处理一个事件,则必须小心不要处理其他事件。如果正在处理不同的事件,则必须在事件处理周围放置 IF 语句以跳过执行代码。

sub dgv_selecteditemchanged()
  If not processing_event
    processing_event = true
    ...
    processing_event = false
  end if
end sub

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.

sub dgv_selecteditemchanged()
  If not processing_event
    processing_event = true
    ...
    processing_event = false
  end if
end sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文