Click here to Skip to main content
13,090,503 members (63,741 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I have a DataTable m_dtDelayedDeliveryDays in my.vb application.With this Data table i loaded my combo box .

The Data table has values like this:
ID DelayDays
6 0
8 3
9 7
10 15
11 20
12 30

Now my combo box contains DelayDays. Based on the user selection say 3 i need ID as 8.
How do i do this in help.

I tried this but it didnt work:

Dim selectedDataRow As Integer = DirectCast(cmbDelayedDeliveryDays.SelectedItem, DataRowView).Row
Dim delayId As Integer = Convert.ToInt32(selectedDataRow(DeliveryDelayDaysID"))

This is throwing an error"Unable to cast object of type 'System.Int32' to type 'System.Data.DataRowView'."

Please help.

Posted 18-Feb-13 8:16am
Updated 19-Feb-13 3:49am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

use the following line of code

int cboIndex = cboCountry.SelectedIndex;
string strId = this.cboCountry.Items[cboIndex].ToString();

for further reference click here[^]
vidkaat 18-Feb-13 15:02pm
Thank u will try
vidkaat 19-Feb-13 9:43am
This case it takes the index as 0 ..It is not returning the ID from my datatable based on the selection made in the combo box
Hasham Ahmad 19-Feb-13 9:57am
can you share the code which is not returning your desired value?
vidkaat 19-Feb-13 10:01am
row = DirectCast(cmbDelayedDeliveryDays.SelectedItem, DataRowView).Row
Dim delayId As Integer = Convert.ToInt32(row("DeliveryDelayDaysID"))
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can use the DataSource binding method to easily access this.
comboBox.DisplayMember  ="DelayDays";//where DelayDays is the column name
comboBox.ValueMember = "ID"; //where ID is the column name
comboBox.DataSource = m_dtDelayedDeliveryDays ;

Now to get the selected value.
string id = comboBox.SelectedValue.ToString();//this will give you the id 8 for the display text 3
vidkaat 18-Feb-13 15:02pm
Thank u will try.
vidkaat 19-Feb-13 9:28am
This didnt work.
Jibesh 19-Feb-13 12:27pm
what you have tried. It works fine for me. may I see your code, your table column Names and how you are trying to get the values.

here is what I tried.

private void Form_Load(object sender, EventArgs e)
DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
dt.Columns.Add(new DataColumn("DelayDays", typeof(Int32)));

dt.Rows.Add(6, 0);
dt.Rows.Add(8, 3);
dt.Rows.Add(9, 7);
dt.Rows.Add(10, 15);

this.comboBox1.ValueMember = "Id";
this.comboBox1.DisplayMember = "DelayDays";
this.comboBox1.DataSource = dt;

private void button1_Click(object sender, EventArgs e)

You should note that column name of the table and the Value/Display member must match. check that too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100