This is a migrated thread and some comments may be shown as answers.

Bind A DataTable To Rad Combobox

3 Answers 421 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 10 Jan 2010, 10:47 PM

Hi,

 

I cannot figure out the syntax to bind an ADO.NET DataTable to a Rad Combobox. Can you please help me by posting some source code on how to accomplish the following task:

 

 I have a DataTable fo countries that has an id column and a name column. I want to show the country name in the combo box but when an item is selected in the combo box, I want to return the id. This is a very common task in asp.net and win forms but I cannot figure out how to do this task using the rad combo box.

 

How would I accomplish something like this using the rad combo box?

 

DataTable dt = GetCountries();

This.cboCountries.DataText = “Country”;

This.cboCountries.DataValue = “id”;

This.cboCountries.DataSource = dt;

This.cboCountries.DataBind();

 

// get the selected id in the combo box

Int id = this.cboCountries.SelectedValue;

 

Can you please post the correct syntax to accomplish this task using the rad combo box.

 

Thanks for the help!

 

Bill

3 Answers, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 12 Jan 2010, 02:07 PM
Hi Bill,

Straight to your question.

To select the value of the items of the RadComboBox (the id) use the SelectedValuePath property. For the data text of the RadComboBox you can use DisplayMemberPath property. The SelectedValue property is the same for the RadComboBox. Below are your lines and the corresponding once in Silverlight.

  • this.cboCountries.DataText = “Country”; -> this.cboCountries.DisplayMemberPath = "Country";
  • this.cboCountries.DataValue = “id”; ->  this.cboCountries.SelectedValuePath = "id";
  • this.cboCountries.DataSource = dt; -> this.cboCountries.ItemsSource = dt;
  • int id = this.cboCountries.SelectedValue; -> this line remains the same.

You don't need this.cboCountries.DataBind(); because when you set the ItemsSource property this will automatically bind the source to the RadComboBox.

If you have further questions please feel free to contact us again.

Regards,
Konstantina
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Farshad
Top achievements
Rank 1
answered on 08 Jan 2015, 07:40 AM
Hi
For the following lines of code :

this.cboCountries.DataSource = dt; -> this.cboCountries.ItemsSource = dt;

I have this error : 
Error 2 Cannot implicitly convert type 'System.Data.DataTable' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) 
0
Kalin
Telerik team
answered on 08 Jan 2015, 12:39 PM
Hi Farshad,

Try setting the ItemsSource as shown below:

this.cboCountries.ItemsSource = dt.DefaultView;

Hope this helps.

Regards,
Kalin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ComboBox
Asked by
Bill
Top achievements
Rank 1
Answers by
Konstantina
Telerik team
Farshad
Top achievements
Rank 1
Kalin
Telerik team
Share this question
or