I have data like this...
id->name->floor
1->store1->1
2->store2->1
3->store2->2
4->store2->3
5->store3->2
I want add it in the combobox
with display like this...
store1
store2
store3
When I choose store2 I want get valuemember = 2, 3, 4
I think a logic (I'm not yet try at code)...
When I choose store2 I just get valuemember = 2
after that I check at database
which id
have same name
in table. But I have 500++ data, so I think it's to waste process
Any idea??
note :
this is just sample form, i don't know how to code, i just make design
it will be helpfull if you give me implementation to combobox not to cmd
Do you want to be easy to retrieve the data in selected items? I prefer to fill data with its special type so that can get data directly from seleted items. because the Combox.Item.Add()
function needs a prameter in Object type, so, I suggest that you can firstly define your data in a new type, like this:
/// <summary>
/// my own data define
/// </summary>
public class MyFloor
{
public int ID { get; set; }
public string Name { get; set; }
public string Floor { get; set; }
//very important, the result will be displayed in the combox
public override string ToString()
{
return string.Format("{0}->{1}->{2}", ID, Name, Floor);
}
}
then, you can fill the data into combox in special type:
void FillData()
{
//load data from txt or database
List<MyFloor> floorList = new List<MyFloor>(){
new MyFloor{ID=1, Name="store1", Floor="1"},
new MyFloor{ID=2, Name="store2", Floor="1"},
new MyFloor{ID=3, Name="store2", Floor="2"},
new MyFloor{ID=4, Name="store2", Floor="3"},
new MyFloor{ID=5, Name="store3", Floor="2"}
};
//fill into combox
foreach (MyFloor floor in floorList)
{
this.comboBox1.Items.Add(floor);
}
}
at last, you can get your data directly:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//retrieve data from selected item as MyFloor object
MyFloor floor = this.comboBox1.SelectedItem as MyFloor;
//show the selected data object
if (floor != null)
{
txtID.Text = floor.ID.ToString();
txtName.Text = floor.Name;
txtFloor.Text = floor.Floor;
}
}
here is my result: