Link to home
Start Free TrialLog in
Avatar of jlrray
jlrray

asked on

Cannot leave focus for databound combobox with lookup table

I created a combobox (either by dragging from the database explorer or manually from the toolbox) that is bound to a personnel table with column called employer.  I have a lookup table with the list of employers and their ID #s.  The combobox control is set with the Datasource pointing to the employers binding source, with the data member the employers name and the value member the employers ID.  When this app is run, i set the combobox to the correct employer - however at this time, i can no longer move focus to any other control and I'm forced to quit the application using the stop button on the debugger.  I find its easy enough to set a lookup on a datagridview, but why is it so difficult for a single combo box?

- I can however, leave the box IF i change the contents of the box manually to an integer instead of the drop down text.
Avatar of jlrray
jlrray

ASKER

I broke down and fixed it using the designer.  I dragged the parent table from the data sources window ONTO the bound combobox already on the form. I still don't know why I couldn't do it manually.  But this solution works if you have lookup tables.  If you want to do it manually?  I dont know.
Avatar of jlrray

ASKER

Here's my solution to fix it manually.  Say that you have the following table:

People
------------
ID (PK)
Name
Status -- was bound to combobox cboStatus

You can create a seperate table for the Status if you want.  But in this case, I went with an array cause I wanted it simple.


I created a class to handle the array.

        private class ComboValString
        {

            private int _id;
            private string _name;

            public ComboValString(int id, string name)
            {
                this._id = id;
                this._name = name;
            }

            public int ID { get { return this._id; } }
            public string Name { get { return this._name; } }

        }

I added the following to the Form_Load event

private void Form1_Load(object sender, EventArgs e)
        {
            //Load Team Combobox
            ArrayList myArray1 = new ArrayList();

            myArray1.Add(new ComboValString(0, "Admin"));
            myArray1.Add(new ComboValString(1, "Red"));
            myArray1Add(new ComboValString(2, "Blue"));
            myArray1.Add(new ComboValString(3, "White"));


            this.cboStatus.DataSource = arr;
            this.cboStatus.DisplayMember = "Name";
            this.cboStatus.ValueMember = "ID";
}

now, if you dragged the combobox from the datasource, you will need to change the
property of databindings from text to selectedvalue.  That is, move the bound column, which in this case would be 'peoplebindingsource - status' from TEXT to SelectedValue.  Thats it.
ASKER CERTIFIED SOLUTION
Avatar of Vee_Mod
Vee_Mod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial