Ehsan Akbar
Ehsan Akbar

Reputation: 7299

combobox filter in c# based onother value

I have 2 comboboxes .txtlocation ,txtstep

I added these item statically.The items of txtlocation are :TO QC TP MAT SUP DCC FIN REC SIG

And the txtstep items are :

TO As SUP
TO As PIP
TO  HW
TO  MOD
TO  FSQ
QC HW
QC LC
QC RE
QC TEST
QC PAD
QC WJCS
TP 
MAT NIS
MAT DATA
SUP ASBUILT
SUP REPORT
SUP REPORT/ASBUILT
DCC MONO LC
DCC MONO RE
FIN LC
FIN PAD
FIN TEST
FIN DRY
FIN FL
FIN RE
REC FIN LC
REC FIN  PAD
REC FIN TEST
REC FIN DRY
REC FIN FL
REC FIN RE
SIG LC
SIG PAD
SIG TEST
SIG DRY
SIG FL
SIG RE

I add an event(selectedindexchang) on txtlocation.if the user select TO the step of TO should be filtered.TO As SUP TO As PIP TO HW TO MOD TO FSQ

Should i use datasource ?

 private void txtlocation_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

Upvotes: 0

Views: 150

Answers (1)

Fabio
Fabio

Reputation: 32463

Different DataSources will be maybe clearer way, you can use Dictionary<TLocation, List<TStep>> for linking selected value with correspondent datasource.

private Dictionary<string, List<string>> _data = new Dictionary<string, List<string>>
{
    { "TO", new List<string> { "TO AS SUP", "TO AS PIP" }},
    { "DCC", new List<string> { "DCC MONO LC", "DCC MONO RE" }},
    { "MAT", new List<string> { "MAT NIS", "MAT DATA" }},
};

comboBoxLocation.DataSource = data.Keys.ToList();

The use SelectedValueChanged eventhandler for setting correct datasource beased on the location selection.

private void comboBoxlocation_SelectedValueChanged(object sender, EventArgs e)
{
    var comboBoxLocations = (ComboBox)sender;
    comboBoxSteps.DataSource = _data[comboBoxLocations.SelectedValue.ToString()];
}

If you have only one list of steps you can filter the list and set filtered result as DataSource.

private void comboBoxlocation_SelectedValueChanged(object sender, EventArgs e)
{
    var comboBoxLocations = (ComboBox)sender;
    var selectedLocation = comboBoxLocations.SelectedValue.ToString();
    comboBoxSteps.DataSource = _AllSteps.Where(step => step.StartsWith(selectedLocation))
                                        .ToList();
}

With this approach you will loop the list every time you made change in the location combobox.

Upvotes: 1

Related Questions