Kevin Fralick

Kevin Fralick

  • NA
  • 21
  • 11k

Setting ValueMember of combobox

Jan 27 2014 10:50 AM

Hi,

I populate a combobox from a recordset of names.  I need for the NAME to display but the value to be another column called PERNO. I think I'm supposed to use ValueMember.

Here's my code:

ds = new SqlDataAdapter("select distinct name, perno from empmast order by name", con);

ds.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
   {
      cbOfficer.Items.Add(dt.Rows[i]["name"]);
   }

cbOfficer.DisplayMember = "name";
cbOfficer.ValueMember = "perno";
officer = cbOfficer.ValueMember;

private void btRefresh_Click(object sender, EventArgs e)
{
    MessageBox.Show(officer);
}

As you can see, I inserted a MessageBox just to see the value returned.  Unfortunately, it is showing the string of "perno" instead of the database value.

I would greatly appreciate your help in showing my mistake.  Thank you.


Answers (6)