decoder
decoder

Reputation: 926

convert combobox.selectedItem to int32 c#

My conversion code is as follows:

para = Int32.Parse(cmbCompany.SelectedItem.ToString());

The data binding code for my combo-box is as so:

string query = "select CompanyID as ID, CompanyName as Name from tblCompany";
comb.ValueMember = "ID";
comb.DisplayMember = "Name";
comb.DataSource = ds.Tables[0];

When I run the above code, I get a conversion error:

How do I solve this problem?

Upvotes: 1

Views: 5976

Answers (3)

decoder
decoder

Reputation: 926

my problem have been solved.i do the following

 Int32.Parse(((DataRowView)cmbCompany.SelectedItem).Row["ID"].ToString());

Upvotes: 0

Adil
Adil

Reputation: 148110

Use comboBox1.SelectedValue

 Int32.Parse(comboBox1.SelectedValue.ToString());

You can use Int32.TryParse if you expect empty value for combo

int number;
bool result = Int32.TryParse(comboBox1.SelectedValue.ToString(), out number);
if (result)
{
     //Your code
}

Upvotes: 2

Talha
Talha

Reputation: 19242

Another technique is to use Convert.ToInt32

Convert.ToInt32(comboBox1.SelectedValue);

Upvotes: 1

Related Questions