Wednesday, September 2, 2020

ComboBox selection changes selection in other comboBox controls

Overview: I have a Windows Form application, built in .Net 4.7.2 using Visual Studio 2019.  The form has 3 PartID combo boxes, allowing the user to select 3 distinct part IDs from a finite list of parts in the drop down.

 

 

Problem: Once the user selects a PartID in any combo box, the other 2 combo boxes are automatically set to that value.

 

 

        private void Start()

        {

            DataTable POLines = GetLineNumsByPO(PO);

 

            FillPartID1(POLines);

            FillPartID2(POLines);

            FillPartID3(POLines);           

        }

 

 

        private void FillPartID1(DataTable poLines)

        {

            AutoCompleteStringCollection acs1 = new AutoCompleteStringCollection();

 

            foreach (var rec in poLines.AsEnumerable())

               acs1.Add(rec.ItemArray[1].ToString());           

 

            cbPartID1.AutoCompleteCustomSource = acs1;

            cbPartID1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            cbPartID1.AutoCompleteSource = AutoCompleteSource.CustomSource;

 

            cbPartID1.DataSource = poLines;

            cbPartID1.DisplayMember = "PART_ID";

            cbPartID1.ValueMember = "PART_ID";

            cbPartID1.SelectedIndex = -1;

        }

 

 

        private void FillPartID2(DataTable poLines)

        {

            AutoCompleteStringCollection acs2 = new AutoCompleteStringCollection();

 

            foreach (var rec in poLines.AsEnumerable())

                acs2.Add(rec.ItemArray[1].ToString());

 

            cbPartID2.AutoCompleteCustomSource = acs2;

            cbPartID2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            cbPartID2.AutoCompleteSource = AutoCompleteSource.CustomSource;

 

            cbPartID2.DataSource = poLines;

            cbPartID2.DisplayMember = "PART_ID";

            cbPartID2.ValueMember = "PART_ID";

            cbPartID2.SelectedIndex = -1;

        }

 

 

        private void FillPartID3(DataTable poLines)

        {

            AutoCompleteStringCollection acs3 = new AutoCompleteStringCollection();

 

            foreach (var rec in poLines.AsEnumerable())

                acs3.Add(rec.ItemArray[1].ToString());

 

 

            cbPartID3.AutoCompleteCustomSource = acs3;

            cbPartID3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            cbPartID3.AutoCompleteSource = AutoCompleteSource.CustomSource;

 

            cbPartID3.DataSource = poLines;

            cbPartID3.DisplayMember = "PART_ID";

            cbPartID3.ValueMember = "PART_ID";

            cbPartID3.SelectedIndex = -1;

        }

 

 

Solution: The Start() method was modified to have 3 separate datatables, each one passed to a partID comboBox.  This resolved one combo box selection updating the other combo boxes

 

        private void Start()

        {

            //3 separate datatables were required to avoid having one PartID selection update another PartID control.

            DataTable POLines1 = GetLineNumsByPO(PO);

            DataTable POLines2 = GetLineNumsByPO(PO);

            DataTable POLines3 = GetLineNumsByPO(PO);

 

            FillPartID1(POLines1);

            FillPartID2(POLines2);

            FillPartID3(POLines3);           

        }

 

1 comment:

  1. In a nutshell, this has to do with the databinding of the datasource. To resolve this, it's best to keep the datasource and datatables separate for each combo box control.

    ReplyDelete