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.
- I can however, leave the box IF i change the contents of the box manually to an integer instead of the drop down text.
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.DisplayMemb er = "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.
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.DisplayMemb
this.cboStatus.ValueMember
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER