How to reinstall combobox in winforms?

I have a Winforms application and combobox has a data source set to DataTable when the form loads. Data is displayed in the drop-down list.

Then, after the user clicks the button, I want to create a new DataTable and assign this datatable as the data source for combobox.

The problem is that after setting the data source as a new data type, the elements in the combobox do not change. Here is the code I'm using.

dlCustomer.DataSource = Nothing
        dlCustomer.DataSource = dtCustomers
        dlCustomer.DisplayMember = "Name"
        dlCustomer.Refresh()

Does anyone know how to make the correct data appear in the combo box the second time I assign it a data source?

+3
source share
1 answer

, , , . ; ComboBox Button:

    Public Class Form1

        Private dtOne As DataTable
        Private dtTwo As DataTable

        Private Sub InitializeTables()
            dtOne = New DataTable("TableOne")
            With dtOne
                .Columns.Add("Text", GetType(String))
                .Columns.Add("Value", GetType(Integer))
            End With

            dtTwo = New DataTable("TableTwo")
            With dtTwo
                .Columns.Add("Text", GetType(String))
                .Columns.Add("Value", GetType(Integer))
            End With

            Dim newRow As DataRow
            For index As Integer = 0 To 2
                newRow = dtOne.NewRow
                newRow.ItemArray = (New Object() {SpellIt(index), index})
                dtOne.Rows.Add(newRow)
            Next

            For index As Integer = 2 To 0 Step -1
                newRow = dtTwo.NewRow
                newRow.ItemArray = (New Object() {SpellIt(index), index})
                dtTwo.Rows.Add(newRow)
            Next

            dtOne.AcceptChanges()
            dtTwo.AcceptChanges()

        End Sub

        Private Shared Function SpellIt(ByVal int As Integer) As String
            Select Case int
                Case 0 : Return "Zero"
                Case 1 : Return "One"
                Case 2 : Return "Two"
                Case Else : Throw New ArgumentOutOfRangeException("Bleh!")
            End Select
        End Function

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            InitializeTables()

            Me.Label1.DataBindings.Add("Text", ComboBox1, "SelectedValue")

            Me.ComboBox1.DataSource = dtOne
            Me.ComboBox1.DisplayMember = "Text"
            Me.ComboBox1.ValueMember = "Value"

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.ComboBox1.DataBindings.Clear()
            Me.ComboBox1.DataSource = Nothing

            Me.ComboBox1.DataSource = dtTwo
            Me.ComboBox1.DisplayMember = "Text"
            Me.ComboBox1.ValueMember = "Value"

        End Sub

    End Class
+4

Source: https://habr.com/ru/post/1707636/


All Articles