Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Issue with DisplayMember and ValueMember

$
0
0

Hi,

I am having a listbox in my WinForm application that I am filling up with items like this:

            List<Metadata> allMetadata = Metadata.GetAllMetadata();

            lbxAllMetadata.DataSource = allMetadata;

            lbxAllMetadata.DisplayMember = "Name";

            lbxAllMetadata.ValueMember = "Id";

In the SelectedIndexChanged event from that listbox I applied the following code:

        private void lbxAllMetadata_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (lbxAllMetadata.SelectedIndex != -1)

            {

                label1.Text = ">" + lbxAllMetadata.SelectedValue.ToString() + "<";

                CurrentItem = Metadata.GetMetadata(Convert.ToInt32(lbxAllMetadata.SelectedValue.ToString()));

            }

        }

Unfortunately this runs into a runtime exception saying:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

What am I doing wrong? How can I get the int-value of the ValueMember property from the listbox?


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>