Problem with Display/Value member of combo box

P

Paolo

Having problems with combo box Display Member and Value Member.

Using a typed dataset I have the following:


private void frmMain_Load(object sender, EventArgs e)
{
cmbxPayee.DisplayMember = dataSet.Payee.P_NameColumn.ToString();
cmbxPayee.ValueMember = dataSet.Payee.P_IdColumn.ToString();
cmbxPayee.DataSource = dataSet.Payee;
}

The combo box is being correctly populated with payee names.

Later I do:

private void cmbxPayee_DisplayMemberChanged(object sender, EventArgs
e)
{

string thePayeeId = cmbxPayee.ValueMember.ToString();
MessageBox.Show(thePayeeId);
}

What is showing in the messageBox is the name of the column (P_Id) not the
value of the column (e.g. "D").

Have I correctly initialised the variable in my frmMain_Load method, and if
so, why is the value of the column not being shown?

Thanks
 
W

Walter Frank

Hi Paolo,

Paolo said:
private void cmbxPayee_DisplayMemberChanged(object sender, EventArgs
e)
{

string thePayeeId = cmbxPayee.ValueMember.ToString();
MessageBox.Show(thePayeeId);
}

You use the wrong event.
Use SelectedValueChanged and get the id from
cmbxPayee.SelectedValue.ToString();

Walter
 
P

Paolo

Walter: thank you. That has worked perfectly.

Walter Frank said:
Hi Paolo,



You use the wrong event.
Use SelectedValueChanged and get the id from
cmbxPayee.SelectedValue.ToString();

Walter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top