Solved

How Do I Get the Currently Selected Item Value from a Bound ComboBox?

Posted on 2007-04-05
4
213 Views
Last Modified: 2010-04-15
I am trying to retrieve the value of a selected item in a combo box, and convert it into an integer. The problem is that the combo box in question is bound to a DataTable. the conversion below doesn't work, because cboDept.SelectedItem is of type System.Data.DataView.Row.

How do I get the actual value of the currently selected item?

Convert.ToInt32(cboDept.SelectedItem)
0
Comment
Question by:marmaxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Yttribium
ID: 18860143
This is  a quick demo I did, to test your question:
From what I see here, you need to reference your "Row" by column:  Hence maybe
Convert.ToInt32(cboDept["column1"].SelectedItem);
If this doesn't help, perhaps look at my sample code below.

If it's a Row type, (I didn't see a row type, only a row collection in my DataView example, but it would be the same) Have a look, see what you can get from this:
                  System.Data.DataView dv = new System.Data.DataView();
                  dv.Table = new DataTable("Data");
                  dv.Table.Columns.Add("col1");
                  dv.Table.Columns.Add("col2");
                  dv.Table.Columns.Add("col3");
                  dv.Table.Rows.Add(new object[] {1,2,3});
                  int i = 5;
                  int a = i + Convert.ToInt32(dv.Table.Rows[0]["col1"]);
                  MessageBox.Show(a.ToString());

The output should be "6".

Let me know if there are problems
0
 
LVL 5

Accepted Solution

by:
Yttribium earned 500 total points
ID: 18860152
*Typo:

Convert.ToInt32(cboDept.SelectedItem["column1"]); not
     -> Convert.ToInt32(cboDept["column1"].SelectedItem);
Sorry about that.

0
 
LVL 1

Expert Comment

by:rgavrilov
ID: 18860334
1. Can you use 'this.comboBox1.Text' instead?
2. Or you can choose a particular column (the same as shown in the combo box, by looking up DispalyMember) of the selected row:
            DataRowView item = (DataRowView) this.comboBox1.SelectedItem;
            string value = (string) item[this.comboBox1.DisplayMember];

0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 18863087
Try cboDept.SelectedValue. This property returns an object value wich you can convert it to what type you need, or the type that is stored in the datatable or dataset.


Hope this helps!

Regards,
Mishu
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question