Hi Scott,
Do you mean you would like to set the selected item to "USA" in the
ComboBox when you look up for id (12) in the value member?
If so, I think my understanding is correct, you may set the value you want
to select into ComboBox.SelectedValue property, if the value exists in the
value member column the match record will be selected as current record, so
that the text in the ComboBox will be changed accordingly.
Here is a simple sample for demonstrate this behavior.
You may create a new Winform application and insert the code snippet below
into the Form1's code, then Call InitTest method in Form1's constructor
(after InitializeComponent).
When you input a different value (1,2,3) in the Textbox and click "GoTo"
button, the SelectedItem in the ComboBox will be changed to Name value
accordingly.
Is this the behavior you are looking for?
Please feel free to reply this thread to let me know if this is not your
expected behavior.
Thanks!
<code>
ComboBox cmb = new ComboBox();
TextBox tb = new TextBox();
Button btn = new Button();
private void InitTest()
{
//Init some test data
ArrayList al = new ArrayList();
al.Add ( new MyObject ( 1, "aaa" ) );
al.Add ( new MyObject ( 2, "bbb" ) );
al.Add ( new MyObject ( 3, "ccc" ) );
//Set DataBinding
cmb.DataSource = al;
cmb.DisplayMember = "Name";
cmb.ValueMember = "ID";
cmb.Location = new Point ( 10, 10 );
//Setup a TextBox to show the Selected Value
tb.Location = new Point(cmb.Location.X + cmb.Width + 10, 10);
tb.Size = new Size ( 50, 10);
//setup a Button to set values into SelectedValue
btn.Text = "GoTo";
btn.Location = new Point( tb.Location.X + tb.Width + 10, 10 );
btn.Size = new Size ( 50, cmb.Height );
btn.Click +=new EventHandler(btn_Click);
this.Controls.Add ( cmb );
this.Controls.Add ( tb );
this.Controls.Add ( btn );
this.Size = new Size ( 275, 100 );
}
private void btn_Click(object sender, EventArgs e)
{
cmb.SelectedValue = Int32.Parse (tb.Text);
}
//Test Data Object
class MyObject
{
public MyObject( int id, string name)
{
_name = name;
_id = id;
}
public String Name
{
get { return _name;}
set { _name = value;}
}string _name ;
public int ID
{
get { return _id;}
set { _id = value;}
}int _id;
}
</code>
Best regards,
Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.