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

Add Items with value and display into comboboxes

7 Answers 830 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Mohan
Top achievements
Rank 1
Mohan asked on 15 Mar 2011, 03:40 AM
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

7 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 15 Mar 2011, 10:12 AM
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
0
Mohan
Top achievements
Rank 1
answered on 15 Mar 2011, 07:24 PM
Hi Richard,

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

Thank you,
Raj
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 15 Mar 2011, 10:25 PM
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
0
Daniel
Top achievements
Rank 1
answered on 30 Mar 2011, 08:31 PM
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.
0
Richard Slade
Top achievements
Rank 2
answered on 31 Mar 2011, 12:18 PM
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
0
Stefan
Telerik team
answered on 04 Apr 2011, 03:40 PM
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
0
clint
Top achievements
Rank 1
answered on 31 Oct 2014, 06:19 AM
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
Tags
DropDownList
Asked by
Mohan
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Mohan
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Stefan
Telerik team
clint
Top achievements
Rank 1
Share this question
or