Click here to Skip to main content
11,928,985 members (54,713 online)
Rate this:
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 8:52am
Edited 3-Feb-13 11:35am
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 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 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.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