Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 302315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:05:10+00:00 2026-05-12T07:05:10+00:00

I have a combobox with Id,Name columns. I have added these values to combobox

  • 0

I have a combobox with Id,Name columns. I have added these values to combobox with datatable:

        DataTable.Rows.Add(1, "Name1");
        DataTable.Rows.Add(2, "Name2");

Id column should be a long. However when I tried to get the Id value, it says cannot convert to long:

long id;
id = this.comboBox1.ValueMember;

How to do that ?

Thanks.


@BlueMonkMN, almost there but when I tried MessageBox.Show prints 1 (from my datatable values above)

        MessageBox.Show(comboBox1.SelectedValue.ToString());

but this line

    id = (long)(comboBox1.SelectedValue);

throws a cast exception ?

Why 1 cannot be converted to long ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:05:10+00:00Added an answer on May 12, 2026 at 7:05 am

    First of all, make sure that a long integer is what you need. A regular integer (int) is specified in the DataTable as a Systemn.Int32 and generally works best on 32-bit operating systems. The range of this integer is from -2,147,483,648 to 2,147,483,647. If in fact you need larger integers than that, then go ahead and use System.Int64 in the DataTable and long in your code.

    Next, you are trying to access the wrong property of the combo box. You need to access the SelectedValue property. ValueMember is the property that determines which column/property of the bound object will be exposed by the SelectedValue property.

    Here is some code illustrating approximately how the combo box should be configured:
    (Generated code from InitializeComponent)

         this.dataTable1BindingSource = new System.Windows.Forms.BindingSource(this.components);
         this.dataSet11 = new WindowsFormsApplication1.DataSet1();
         // 
         // comboBox1
         // 
         this.comboBox1.DataSource = this.dataTable1BindingSource;
         this.comboBox1.DisplayMember = "Name";
         this.comboBox1.ValueMember = "id";
         // 
         // dataTable1BindingSource
         // 
         this.dataTable1BindingSource.DataMember = "DataTable1";
         this.dataTable1BindingSource.DataSource = this.dataSet11;
    

    And here’s some code illustrating how you retrieve the selected value from the combo box, if in fact you want the id to be a long integer:

         long id = (long)(comboBox1.SelectedValue);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the DataTable with following columns: id, Name, Description, ParentId and would like
I have a datatable with one column that contain names.I want to load combobox
I have a combobox that has the entries of a full name, eg: John
At the moment I have a combobox that is populated from the name fields
I have a combobox in a form and a button. I want to add
I have tables like that in Datagridview: Name Money ------------- Hi 100 //here Combobox
I have WPF combobox bound to a ObservableCollection ItemCategoryList <ComboBox Grid.Column=1 Grid.Row=2 Height=Auto HorizontalAlignment=Stretch
I have this columns in datagrid wpf <DataGrid.Columns> <DataGridComboBoxColumn SelectedItemBinding={Binding Path=ProductId} DisplayMemberPath=Name SelectedValuePath=Id />
I have an excel file which has two columns (1. Name and 2. Value)
I have populated datagridview using the following method and I have added button column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.