Page 163 - CSharp/C#
P. 163

Chapter 20: BindingList




        Examples



        Avoiding N*2 iteration


        This is placed in a Windows Forms event handler


         var nameList = new BindingList<string>();
         ComboBox1.DataSource = nameList;
         for(long i = 0; i < 10000; i++ ) {
             nameList.AddRange(new [] {"Alice", "Bob", "Carol" });
         }


        This takes a long time to execute, to fix, do the below:


         var nameList = new BindingList<string>();
         ComboBox1.DataSource = nameList;
         nameList.RaiseListChangedEvents = false;
         for(long i = 0; i < 10000; i++ ) {
             nameList.AddRange(new [] {"Alice", "Bob", "Carol" });
         }
         nameList.RaiseListChangedEvents = true;
         nameList.ResetBindings();



        Add item to list


         BindingList<string> listOfUIItems = new BindingList<string>();
         listOfUIItems.Add("Alice");
         listOfUIItems.Add("Bob");


        Read BindingList online: https://riptutorial.com/csharp/topic/182/bindinglist-t-


































        https://riptutorial.com/                                                                             109
   158   159   160   161   162   163   164   165   166   167   168