-2

Here is the data it is connected to:

enter image description here

Here is the combo box set up:

enter image description here

What I am calling

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(comboBox1.ValueMember);
}

This shows up:

enter image description here

But the combo description works fine:

enter image description here

Why is the messagebox not showing 1 or 2?

Ajit Panigrahi
  • 752
  • 10
  • 27
Eduards
  • 1,734
  • 2
  • 12
  • 37
  • 2
    Have you tried `SelectedValue`, since you are evidently binding *or* `SelectedItem` *if NOT binding*? I can't see pictures here on my end either, it's best if they contain errors and what not, to post them in your post... IMHO, [**have**](https://learn.microsoft.com/en-us/archive/blogs/jaredpar/combobox-selecteditem-selectedvalue-selectedwhat) a read there about this. – Trevor Feb 10 '20 at 16:09
  • `ValueMember` = the member you have Bound your Combobox to, but not the current value it is, have a look at your second Image, what did you write in "Value Member"? and thats what you are getting with your code – Rand Random Feb 10 '20 at 16:10
  • `MessageBox.Show(comboBox1.SelectedValue);` should give you the results you are looking for. When in doubt, set a breakpoint on the `button1_Click` method and inspect the state of your `comboBox1` object. More than likely, the data you want is there, but you're referencing the wrong field. – h0r53 Feb 10 '20 at 16:17
  • Hi all.. no it says `cannot convert from object to string` – Eduards Feb 10 '20 at 16:26
  • https://stackoverflow.com/questions/6901070/getting-selected-value-of-a-combobox just a thought, this has been answered before. – Trevor Feb 10 '20 at 16:27

1 Answers1

2

Use SelectedValue property which gets the value of ValueMember property which in your case is privilege

Gets or sets the value of the member property specified by the ValueMember property.

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(comboBox1.SelectedValue.ToString());
}
Clint
  • 6,011
  • 1
  • 21
  • 28