3

我有两个组合框。combobox1 的数据源是一个固定的字符串列表。Combobox2 的数据源将是一个字符串列表,该列表取决于对 combobox1 的选择。这与以下常见情况非常相似:首先输入您的国家,然后根据您的输入,第二个组合框将显示该国家/地区的大学列表。

'Change selection of first combobox
Private Sub cbxClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxClient.SelectedIndexChanged
  Try
    If cbxClient.SelectedIndex <> -1 Then
      GetAccount()
    End If
  Catch
    Throw
  End Try
End Sub

'Based on selection of first combobox, update the data sorce of second combobox
Private Sub GetAccount()
  Try
    m_listAccount.Clear()

    Dim strClient As String = cbxClient.SelectedItem.ToString

    For i As Integer = 0 To m_listDS.Count - 1
      If m_listDS(i).Client.Tostring = strClient Then
        m_ds = m_listDS(i)
        Exit For
      End If
    Next

    If Not m_ds Is Nothing Then
      For Each row As DataRow In m_ds.Tables("Account").Rows
        m_listAccount.Add(row("account").ToString)
      Next
    End If

    cbxAccount.DataSource = m_listAccount

  Catch ex As Exception
  End Try
End Sub

我的问题是,虽然 m_listAccount 更新(具有正确的信息),但 cbxAccount 中显示的选项并未根据 m_listAccount 更新(具有错误的信息)。我不明白为什么会这样。

注意:假设 m_listAccount 中的旧字符串为 {"old1"}(列表只有 1 个字符串),更新后 m_listAccount 中的字符串为 {"new"}。通过断点,我得到以下信息:

cbxAccount.DataSorce={"new"}
cbxAccount.SelectedItem={"old1"}

在表单中,cbxAccount 显示“old1”字符串。

4

2 回答 2

1

尝试这个,

cbxAccount.DataSource = Nothing
cbxAccount.DataSource = m_listAccount

如果您只是更新同一个列表对象,则数据源可能不会自动更新,但如果指针更改为数据源,则应该自动更新。

或者你可以试试 m_listAccount.AcceptChanges()

如果m_listAccount是一个DataTable

于 2012-05-15T16:43:46.587 回答
0

看起来您实际上并没有调用数据绑定方法,即

cbxAccount.DataBind()

尝试在设置数据源之后放置它。

于 2012-05-15T15:58:43.203 回答