Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 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 vb.net..Please 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.



Thanks.
Posted
Updated 19-Feb-13 3:49am
v3

use the following line of code

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


for further reference click here[^]
 
Share this answer
 
Comments
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"))
You can use the DataSource binding method to easily access this.
C#
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.
C#
string id = comboBox.SelectedValue.ToString();//this will give you the id 8 for the display text 3
 
Share this answer
 
v2
Comments
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)
{
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}

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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900