2013-12-17 70 views
1

我想根據ToolStripComboBox中的選擇來填充TableAdapter。 首先,我想通過將ToolStipComboBox綁定到數據源來填充ToolStipComboBox。然後一旦填滿,我想填充TableAdapter。從綁定工具中填充TableAdapterStripComboBox

這是我的代碼:

Private Sub ToolStripComboBox_MessageType_Click(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.Click 
    Me.ToolStripComboBox_MessageType.ComboBox.DataSource = DataSet_UToolDb.XML_MESSAGE_TYPE 
    Me.ToolStripComboBox_MessageType.ComboBox.DisplayMember = "MessageType" 
    Me.ToolStripComboBox_MessageType.ComboBox.ValueMember = "MTId" 
End Sub 

Private Sub ToolStripComboBox_MessageType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.SelectedIndexChanged 
    Me.TableAdapter_XML_MESSAGE_STRUCTURE.Fill(DataSet_UToolDb.XML_MESSAGE_STRUCTURE, Me.ToolStripComboBox_MessageType.ComboBox.SelectedValue) 
End Sub 

出於某種原因(如果我通過代碼步驟)的代碼從那裏數據源設置的跳躍,到TableAdapter的填充。這是因爲TableAdapter的select查詢正在根據從ToolStipComboBox中選擇的值查找值而導致異常。

回答

1

我懷疑你的代碼跳到TableAdapter被填充的位置,因爲通過設置組合框的DataSource導致SelectedIndexChanged事件被觸發。

因此,如果ToolStripComboBox尚未填充,您需要告訴SelectedIndexChanged處理程序返回,您可以通過在填充ToolStripComboBox時設置布爾標誌來執行此操作。例如:

Dim m_ToolStripComboBoxPopulated As Boolean 

Private Sub ToolStripComboBox_MessageType_Click(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.Click 
    Me.m_ToolStripComboBoxPopulated = False 
    Me.ToolStripComboBox_MessageType.ComboBox.DataSource = DataSet_UToolDb.XML_MESSAGE_TYPE 
    Me.ToolStripComboBox_MessageType.ComboBox.DisplayMember = "MessageType" 
    Me.ToolStripComboBox_MessageType.ComboBox.ValueMember = "MTId" 
    ' Indicate ToolStripComboBox has been populated 
    Me.m_ToolStripComboBoxPopulated = True 
End Sub 

Private Sub ToolStripComboBox_MessageType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ToolStripComboBox_MessageType.SelectedIndexChanged 
    If (Me.m_ToolStripComboBoxPopulated = False) Then 
     Return 
    End If 
    Me.TableAdapter_XML_MESSAGE_STRUCTURE.Fill(DataSet_UToolDb.XML_MESSAGE_STRUCTURE, Me.ToolStripComboBox_MessageType.ComboBox.SelectedValue) 
End Sub