Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# list ListView bind
private Animal AddAnimal()
        {
            string name = txtName.Text;
            double age = Convert.ToDouble(txtAge.Text);
 
            CategoryType animalCategory = (CategoryType)lstCategory.SelectedIndex;
            Animal animalItem = null;
 
            switch (animalCategory)
            {
                case CategoryType.Bird:
                    Bird.BirdSpecies birdSpecie = (Bird.BirdSpecies)Enum.Parse(typeof(Bird.BirdSpecies), lstCategory.SelectedItem.ToString());
                    animalItem = Bird.BirdFactory.CreateBird(birdSpecie);
                    break;
                    
                case.... etc.
 
My problem mainly revolves around trying to bind the "new" datasource for my second list depending on the choice in the first list. For instance if the bird category is chosen, how would I go about to bind the specific enums (e.g. bee, butterfly) to the second list? Do I need to use the eventhandler SelectedIndexChanged, if so, how would that be done most plausible?
 
Having the method run under SelectedIndexChanged seems to work. However, the binding part is more troublesome, I can't figure it out. It seems abundant to have "lstCategory.SelectedItem" go off as the string value in Enum.Parse, or am I out in the dark?
Posted 3-Feb-13 8:52am
Edited 3-Feb-13 11:35am
v5
Comments
Sergey Alexandrovich Kryukov at 3-Feb-13 20:27pm
   
The whole idea looks so wrong! You replace the classification with enumeration and switch. Looks like a total abuse. I would advise you to review your whole type system...
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to use the onselectedindexchanged event to select your new category and then run your add animal from there.
  Permalink  
Comments
leprechauny at 3-Feb-13 16:27pm
   
Ye, I assumed that would be part of it. And it seems to work. However, how should I successfully bind it - as you can see the first list is called lstCategory and the second is lstAnimalType - and I can't seem to figure it out. The 'Enum.Parse' just doesn't do it for me. Any advice?
milenalukic at 3-Feb-13 16:57pm
   
I would first load the data source for list category and populate this list. Then on selected index changed run the data source for the animal with the selected category and populate the animal list.
 
Normally I would use list.items.add to populate the lists.
 
For(int i=0;i
leprechauny at 3-Feb-13 18:41pm
   
thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Maciej Los 305
2 Richard MacCutchan 270
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,834
1 Sergey Alexandrovich Kryukov 7,477
2 DamithSL 5,689
3 Maciej Los 5,329
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100