2011-03-06 153 views
1

我使用的是vb.net和winforms。Vb.net/DataGridView/ComboBox列

我有一個綁定DataGridView的窗體。在DGV上,我有5列ComboBox。我正在使用EditingControlShowing甚至可以捕獲組合框選擇。 (見下面的代碼)。 這裏是問題:

當我用一個組合框單擊一個單元格,然後進行選擇,然後更新基礎單元格(單元格=選定的值),然後單擊另一個DGV行就會出現故障。如果在我更新數據源的相應行上的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 
+0

您是否在DataGridView中具有唯一標識符?如果是這樣,您可以使用該值查找數據集中的相應行。 – N0Alias 2011-03-07 01:31:07

+0

嘗試'MyDataSet.Tables(0).Rows(Me.DataGridView1.CurrentRow.Index).EndEding' – NeverHopeless 2014-03-02 21:14:04

回答

0

被解僱的事件正在重新進入。如果您正在處理某個事件,則必須小心不要處理其他事件。如果正在處理不同的事件,則必須在處理事件時放置IF語句以跳過執行代碼。

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