1

我正在尝试根据 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 的选择查询正在根据从 ToolStipComboBox 中选择的值查找值。

4

1 回答 1

1

我怀疑您的代码正在跳转到填充 TableAdapter 的位置,因为通过设置 ComboBox 的 DataSource 会导致SelectedIndexChanged事件被触发。

SelectedIndexChanged因此,如果ToolStripComboBox 尚未填充,您需要告诉处理程序返回,您可以通过在填充 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
于 2013-12-18T10:11:20.317 回答