Click here to Skip to main content
12,897,431 members (53,490 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
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 19-Feb-13 3:56am
Updated 19-Feb-13 4:05am
richcb 19-Feb-13 10:07am
You can't cast an Int32 into a DataRowView. Why not just do this: Dim selectedDataRow As Integer = Convert.ToInt32(cmbDelayedDeliveryDays.SelectedValue)
vidkaat 19-Feb-13 10:09am
Dim row As DataRow
row = DirectCast(cmbDelayedDeliveryDays.SelectedItem, DataRowView).Row
Dim delayId As Integer = Convert.ToInt32(row("DeliveryDelayDaysID"))

Even this does not work ..i get the error "Unable to cast object of type 'System.Int32' to type 'System.Data.DataRowView'."
vidkaat 19-Feb-13 10:12am
I need to get the ID based on the value selected from the Combo box. I dont need the selected value directly.
richcb 19-Feb-13 10:33am
I understand now. Try this: DirectCast(cmbDelayedDeliveryDays.SelectedItem, DataRowView).Item("DeliveryDelayDaysID").
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

When you load the datatable into your combobox define the DisplayMember and ValueMember
ComboBox1.DataSource = myDataTable
ComboBox1.DisplayMember = "DelayDays"
ComboBox1.ValueMember = "ID"
Then on the appropriate comboBox event you can use (e.g.)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Code sample like as follows
<asp:dropdownlist id="ddlDays" runat="server" xmlns:asp="#unknown">
<asp:button id="btnSave" text="Save" runat="server" onclick="btnSave_Click" xmlns:asp="#unknown" />

Page load event
protected void Page_Load(object sender, EventArgs e)
    if (!base.IsPostBack)
        ddlDays.DataSource = GetDataTable();
        ddlDays.DataTextField = "DelayDays";
        ddlDays.DataValueField = "ID";

Save button click event handler
protected void btnSave_Click(object sender, EventArgs e)
    int selectedId = int.Parse(ddlDays.SelectedValue);

sample data provider method
private DataTable GetDataTable()
    var dt = new DataTable("DataTabl1");
    dt.Columns.Add("ID", typeof(Int32));
    dt.Columns.Add("DelayDays", typeof(Int32));
    DataRow row1 = dt.NewRow();
    row1["ID"] = 6;
    row1["DelayDays"] = 0;
    row1 = dt.NewRow();
    row1["ID"] = 8;
    row1["DelayDays"] = 3;
    row1 = dt.NewRow();
    row1["ID"] = 9;
    row1["DelayDays"] = 7;
    return dt;

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 | Mobile
Web01 | 2.8.170424.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