0

I have 3 Dropdown Lists (could be more in future).

DropdownList1 is a parent and is populated from a database.
DropdownList2 is a child and is populated from the selected item from DropdownList1.
DropdownList3 is a child and is populated from the selected item from DropdownList2.

I would like to add the selected item to a ListBox.

The problem i have is DropdownList2 and DropdownList3 may not always have a child item, so to add the items to a ListBox, i could store their IDs but display their text.

My database table has 3 columns to save the value (ID) from the 3 dropdown lists.

My question is if i display the text of the item in the ListBox, the next step is to save the data into the table into the relevant columns (so column 1 would save data from DD1, Column 2 would save from DD2 etc) is there anyway to identify which ID the item belongs to, so i can save the correct ID to the correct column?

So a user selects item 1 from DD1 item 2 from DD2, there is no item available from DD3, so they click add which adds the item to the ListBox. When i have to save this item how could i distinguish the item added was upto DD2 therefore it needs to be added to Column2? Or is there a better approach?

Hope this makes sense

6
  • you could assign the selections to objects, then add them to an array and link that to the listbox. That way you could do obj[i].val1, obj[i].val2 etc to access the values for your query (were i = method to extract array location) Commented Feb 4, 2016 at 13:05
  • So do you mean create a new class with the properties for dd1,2,3 when they click add assign which dd it's coming from to this class and use this class to save to the appropriate field on the db? Commented Feb 4, 2016 at 13:13
  • yea. So when you create the item to add to the listbox, do soemthing like myObj object = new myObj(val1, val2, val3) then add it to an array or list Commented Feb 4, 2016 at 13:16
  • Ok sounds good let me try this out. Commented Feb 4, 2016 at 13:22
  • If it suits your needs, let me know and ill write up an answer for you to flag Commented Feb 4, 2016 at 13:22

1 Answer 1

2

List boxes don't like to store multiple values. They can, however, store objects as their datasource.

First create a custom object, then assign the values from each of your dropdown lists to the object:

Declare a new list of your custom object first

List<myObject> list = new List<myObject>();

then assign this to your button:

private void Button1_Click(object sender, EventArgs e)
{

    myObject object = new myObject(Dropdown1, Dropdown2, Dropdown3);
    list.Add(object);

    //assign the list to the Listbox control
    ListBox.Datasource = list;

}

When you want to access the values for your query, use a foreach loop to cycle over the objects in the list, with a pointer value to access each of them.

foreach (myObject obj in list)
{
    var val1 = list[obj].Dropdown1;
    var val2 = list[obj].Dropdown2;
    var val3 = list[obj].Dropdown3;

    //enter your sql code here
}

Let me know if you want to expand further.

Sign up to request clarification or add additional context in comments.

1 Comment

This did the trick. The only thing i had to do was add a name property to the class to display a friendly name. I used the selectedIndex method to remove the item selected in the Lbox (just in case this thread finds anyone who needs to do this)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.