Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Changing DataSource on ComboBox is slow

$
0
0

I have a Master-Detail type form.  For the Detail data I have about 12 ComboBoxes that when the master data changes I change the DataSource (and SelevtedValue) for each of the ComboBoxes.  Each ComboBox DataSource has from about 5 to 50 items in a List.  Changing the master data from one record to the next via the user clicking a button to go to the next record is quite slow.  I made the below simple example that demonstres this 'slowness'.  It simply loads the People List with 40 Persons, then that List is used for the dataSource for a ComboBox.  Clicking on the form causes it to run through 12 iterations of setting the the DataSource to Nothing then back to the People List to simulate 12 different ComboBoxs.  This simple process takes (on my system) 2.2 seconds.  This really slows the 'scrolling' thorugh the master data.

Is there anything I can do to make changing the DataSource not take so much time?

Thanks for any ideas!

Curtis

Public Class Form1
  Private People As New List(Of Person)
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim p As Person
    For i As Integer = 1 To 40
      p = New Person
      p.Nm = "Person " & i
      People.Add(p)
    Next

    Dim T As New ComboBox
    T.Name = "cb"
    CType(T, ComboBox).DisplayMember = "Nm"
    CType(T, ComboBox).DataSource = People
    Me.Controls.Add(T)
  End Sub

  Private Sub Me_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Click
    Dim s As New Stopwatch
    s.Start()
    For i As Integer = 1 To 12
      CType(Me.Controls("cb"), ComboBox).DataSource = Nothing
      CType(Me.Controls("cb"), ComboBox).DisplayMember = "Nm"
      CType(Me.Controls("cb"), ComboBox).DataSource = People
    Next
    MsgBox("elapsed time: " & s.ElapsedMilliseconds)
  End Sub
End Class

Public Class Person
  Private _Nm As String
  Public Property Nm() As String
    Get
      Nm = _Nm
    End Get
    Set(ByVal value As String)
      _Nm = value
    End Set
  End Property
End Class


Viewing all articles
Browse latest Browse all 2535

Trending Articles