wpf - Convert C# ObservableCollection sample to VB.NET dataset or equivalent -


i have c# example populating multiselect combobox. example found online. works fine static values coming observable collection.

i want change database driven sql server backend, having issues. did populate combobox, selection acting kind of screwy. here sample code if help. type of simple query northwind table suffice such as: "select categoryname categories".

so question is: how convert observablecollection use query database rather static string list used in example below? while implementing same property changed functionality.

class datasource : inotifypropertychanged {      #region inotifypropertychanged members      public event propertychangedeventhandler propertychanged;     private void onpropertychanged(string propertyname)     {         if (propertychanged != null)             propertychanged(this, new propertychangedeventargs(propertyname));     }      #endregion      private observablecollection<string> _animals = new observablecollection<string> { "cat", "dog", "bear", "lion", "mouse", "horse", "rat", "elephant", "kangaroo", "lizard", "snake", "frog", "fish", "butterfly", "human", "cow", "bumble bee" };      public observablecollection<string> animals     {         { return _animals; }     }      private string _selectedanimal = "cat";     public string selectedanimal     {         { return _selectedanimal; }         set         {             _selectedanimal = value;             onpropertychanged("selectedanimal");         }     }      private observablecollection<string> _selectedanimals;     public observablecollection<string> selectedanimals     {                 {             if (_selectedanimals == null)             {                 _selectedanimals = new observablecollection<string> { "dog", "lion", "lizard" };                 selectedanimalstext = writeselectedanimalsstring(_selectedanimals);                 _selectedanimals.collectionchanged +=                     (s, e) =>                     {                         selectedanimalstext = writeselectedanimalsstring(_selectedanimals);                         onpropertychanged("selectedanimals");                     };             }             return _selectedanimals;         }         set         {             _selectedanimals = value;         }     }      public string selectedanimalstext     {         { return _selectedanimalstext; }         set         {             _selectedanimalstext = value;             onpropertychanged("selectedanimalstext");         }     } string _selectedanimalstext;       private static string writeselectedanimalsstring(ilist<string> list)     {         if (list.count == 0)             return string.empty;          stringbuilder builder = new stringbuilder(list[0]);          (int = 1; < list.count; i++)         {             builder.append(", ");             builder.append(list[i]);         }          return builder.tostring();     } } 

i'm not sure if understand correctly, ...

have considered using linq data entity?

add ado.net data entity model project , use wizard connect data source.

then write linq statement selects columns wish entity object , set itemsource

example:

mydataentity context = new mydataentity();  list<animals> myanimals = new list<animals>(); myanimals = context.animals.tolist;  var myitemsource =         in myanimals         select new { selectedanimal = a.animal, selectedanimaltext = a.animaltext }; 

the linq select statement creates anonymous type containing 2 fields columns if set itemsource i.e: selectedanimal , selectedanimaltext

now set combobox's itemsource property

combobox.itemssource == myitemsource; 

and you're done!


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -