Vb.net / DataGridView / ComboBox列

时间:2011-03-06 21:07:17

标签: vb.net datagridview qcombobox

我正在使用vb.net和winforms。

我有一个带有Bound DataGridView的表单。在DGV上我有5列ComboBox。我正在使用EditingControlShowing甚至捕获ComboBox选择。 (见下面的代码)。 这是问题所在:

单击带有ComboBox的单元格并进行选择,然后更新基础单元格(单元格=选定值),然后单击DGV的另一行,它会变得混乱。如果我在DataSource的相应行上更新Cell I do和EndEdit后,它似乎可以找到。

如何确定相应的数据源行,以便我可以自动执行此操作?

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

1 个答案:

答案 0 :(得分:0)

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

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