Add Items with value and display into comboboxes

8 posts, 2 answers
  1. Mohan
    Mohan avatar
    5 posts
    Member since:
    Jan 2009

    Posted 14 Mar 2011 Link to this post

    Hi

    I was looking for a way to add items to a radcombobox with a value and a different displayvalue.

    For example Display member with Customer Name & Value member with Customer Id

    Thanks

    Raj
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 15 Mar 2011 Link to this post

    Hello Raj,

    To do this you will need to provide a datasource that you can use as a Display and Value member. The RadListDataItem simply has a text property that is used for both. So, using a datasource exmaple would be as follows

    public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         private BindingList<Person> people = new BindingList<Person>();
         private void Form1_Load(object sender, EventArgs e)
         {
             people.Add(new Person("Richard", 1));
             people.Add(new Person("Bob", 2));
             this.radDropDownList1.DataSource = people;
             this.radDropDownList1.DisplayMember = "Name";
             this.radDropDownList1.ValueMember = "Id";
         }
     }
     public class Person
     {
         public Person(string name, int id)
         {
             this.Name = name; this.Id = id;
         }
         public string Name
         {get; set;}
         public int Id
         {get; set;}
     }

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mohan
    Mohan avatar
    5 posts
    Member since:
    Jan 2009

    Posted 15 Mar 2011 Link to this post

    Hi Richard,

    Thanks for the reply, Is it possible you can send me the code in VB.Net please.

    Thank you,
    Raj
  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 15 Mar 2011 Link to this post

    Hi Raj,

    Here is the code in VB.NET
    Public Partial Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub
        Private people As New BindingList(Of Person)()
        Private Sub Form1_Load(sender As Object, e As EventArgs)
            people.Add(New Person("Richard", 1))
            people.Add(New Person("Bob", 2))
            Me.radDropDownList1.DataSource = people
            Me.radDropDownList1.DisplayMember = "Name"
            Me.radDropDownList1.ValueMember = "Id"
        End Sub
    End Class
    Public Class Person
        Public Sub New(name As String, id As Integer)
            Me.Name = name
            Me.Id = id
        End Sub
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
        Public Property Id() As Integer
            Get
                Return m_Id
            End Get
            Set
                m_Id = Value
            End Set
        End Property
        Private m_Id As Integer
    End Class

    For reference, you might be interested to know about the Telerik Code Converter which can convert to and from C#/vb.net

    Hope that helps. Please remember to mark as answer and if you have further questions, then please let me know
    Thanks
    Richard
  6. Daniel
    Daniel avatar
    4 posts
    Member since:
    Jun 2009

    Posted 30 Mar 2011 Link to this post

    Why can't I use  IList instead ofBindingList ?

    When I try to use IList, raddropdownlist display the name of my class, following the example, it would be "Person" display several times.

    Could you help me to understand why?

    Thanks.

    Best Regards.
  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 31 Mar 2011 Link to this post

    Hello,

    You can use List instead of BindingList. If you take the first exmaple in this thread and replace BindingList with List, then it also works without issue. If you need further help, please let me know.
    Thanks
    Richard
  8. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 04 Apr 2011 Link to this post

    Hi Daniel,

    Thank you for writing.

    Please refer to the beginning of this help article, where you can find the answer to your question. If you need additional information, you might find this MSDN article helpful.

    I hope this helps.

    All the best,
    Stefan
    the Telerik team
  9. clint
    clint avatar
    1 posts
    Member since:
    Oct 2014

    Posted 31 Oct 2014 Link to this post

    You can use a Dictionary Object and solev this issue..

                Dictionary comboSource = new Dictionary();
                comboSource.Add("1", "Sunday");
                comboSource.Add("2", "Monday");
                comboBox1.DataSource = new BindingSource(comboSource, null);
                comboBox1.DisplayMember = "Value";
                comboBox1.ValueMember = "Key";

    Full Source:

    http://net-informations.com/q/faq/combovalue.html

    Clint
Back to Top
UI for WinForms is Visual Studio 2017 Ready