Click here to Skip to main content
11,803,019 members (48,113 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ListView Binding
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);
                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 7:52am
Edited 3-Feb-13 10:35am
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...

1 solution

Rate this: bad
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.
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 335
1 KrunalRohit 291
2 F-ES Sitecore 270
3 CPallini 270
4 Sergey Alexandrovich Kryukov 234
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,735
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 3 Feb 2013
Copyright © CodeProject, 1999-2015
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