As I understood you are losing the Id of the state when you just put its name in the Combobox.
set your first combobox to display the 'Name' and have 'Id' as value:
combo_states.ValueMember = "Id";
combo_states.DisplayMember = "Name";
foreach(string Line in States)
{
string[] state_element = Line.Split(';');
combo_states.Items.Add(new { Id = state_element [0], Name = state_element[1]});
}
Now in your combobox's on SelectedValueChange event you can access the Id of the state like:
string id = combo_states.SelectedValue;
and having districts like:
var districts = File.ReadAllLines("district.txt")
.Select(x =>
{
string split = x.Split(';');
return new {StateId = split[0], DistrictId = split[1], DistrictName = split[1]}
};
Then:
string id = combo_states.SelectedValue;
district_combo.ValueMember = "DistrictId";
district_combo.DisplayMember = "DistrictName";
foreach(var item in districts.Where(d => d.StateId == id))
{
district_combo.Items.Add(item);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…