从Bound ToolStripComboBox填充TableAdapter

时间:2013-12-17 10:10:47

标签: vb.net datasource tableadapter toolstripcombobox

我正在尝试根据ToolStripComboBox中的选择来填充TableAdapter。 首先,我想通过将ToolStipComboBox绑定到数据源来填充它。然后一旦填充,我想填充TableAdapter。

这是我的代码:

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 个答案:

答案 0 :(得分:1)

我怀疑你的代码跳转到填充TableAdapter的位置,因为通过设置ComboBox的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