Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

auto select new value in combobox

Status
Not open for further replies.

itlee

Programmer
Feb 14, 2001
34
CH
I have a combobox on form1 with a button to open form2 where the user can create a new item for the combobox.

both forms are databound to a database and when form2 closes the datasource for the combobox is refreshed.

How can i pass back the id of the new item so the combox auto selects the new item?

itlee.

itlee. MCP\Analyst\Programmer\SQL\.NET\VB\C#
 
I worked it out!

I created a property in Form2 to hold the value of the new item.

Then in Form1, after reloading the bindingsource, do a search on the bindingsource for the value from Form2.property to return its index in the list and assign that index to SelecedIndex of the combobox.

Code:
            MyForm form = new MyForm();
            form.ShowDialog();

            // Reload the combobox.
            this.mydataTableAdapter.Fill(this.mydataDataSet.mytable);

            // Find the new value and display in the combobox.
            if (mybindingsource.SupportsSearching == true)
            {
                int foundIndex = mybindingsource.Find("Name", form.thenewitemproperty);
                if (foundIndex > -1)
                {
                    thecombobox.SelectedIndex = foundIndex;
                }
            }

itlee. MCP\Analyst\Programmer\SQL\.NET\VB\C#
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top