Bind A DataTable To Rad Combobox

4 posts, 0 answers
  1. Bill
    Bill avatar
    21 posts
    Member since:
    May 2008

    Posted 10 Jan 2010 Link to this post

    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

  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 12 Jan 2010 Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Farshad
    Farshad avatar
    1 posts
    Member since:
    Dec 2014

    Posted 08 Jan 2015 in reply to Konstantina Link to this post

    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?) 
  5. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 08 Jan 2015 Link to this post

    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.

     
Back to Top