Click here to Skip to main content
11,641,971 members (67,054 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 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 19-Feb-13 3:56am
vidkaat561
Edited 19-Feb-13 4:05am
v2
Comments
richcb at 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 at 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 at 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 at 19-Feb-13 10:33am
   
I understand now. Try this: DirectCast(cmbDelayedDeliveryDays.SelectedItem, DataRowView).Item("DeliveryDelayDaysID").
Rate this: bad
good
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.)
Debug.Print(ComboBox1.SelectedValue.ToString()
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

Page load event
protected void Page_Load(object sender, EventArgs e)
{
    if (!base.IsPostBack)
    {
        ddlDays.DataSource = GetDataTable();
        ddlDays.DataTextField = "DelayDays";
        ddlDays.DataValueField = "ID";
        ddlDays.DataBind();
    }        
}
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;
    dt.Rows.Add(row1);
 
    row1 = dt.NewRow();
    row1["ID"] = 8;
    row1["DelayDays"] = 3;
    dt.Rows.Add(row1);
 
    row1 = dt.NewRow();
    row1["ID"] = 9;
    row1["DelayDays"] = 7;
    dt.Rows.Add(row1);
    return dt;
}
  Permalink  

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

  Print Answers RSS
0 DamithSL 268
1 OriginalGriff 150
2 Sergey Alexandrovich Kryukov 134
3 Mika Wendelius 130
4 Afzaal Ahmad Zeeshan 89
0 Mika Wendelius 460
1 OriginalGriff 265
2 DamithSL 258
3 CPallini 169
4 Afzaal Ahmad Zeeshan 149


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
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