Hello, Pierre-Jean,
Setting the
DataSource property of
RadDropDownList is expected to set the selection to the first item in the applied collection. However, have in mind that the
SelectedValue will store the respective field's value that is specified as
ValueMember. If the
ValueMember was not specified at the time of setting the
DataSource, you won't get the expected
SelectedValue. That is why I would recommend you to specify the
DisplayMember and
ValueMember first and then set the
DataSource.
public RadForm1()
{
InitializeComponent();
List<Item> items = new List<Item>();
for (int i = 1; i <= 5; i++)
{
items.Add(new Item(i*10,"Item" + i*10));
}
this.radDropDownList1.SelectedIndexChanged+=radDropDownList1_SelectedIndexChanged;
this.radDropDownList1.SelectedValueChanged+=radDropDownList1_SelectedValueChanged;
this.radDropDownList1.DisplayMember = "Description";
this.radDropDownList1.ValueMember = "ItemCode";
this.radDropDownList1.DataSource = items;
}
private void radDropDownList1_SelectedValueChanged(object sender, EventArgs e)
{
Console.WriteLine("SelectedValue: "+ this.radDropDownList1.SelectedValue);
}
private void radDropDownList1_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
Console.WriteLine("SelectedIndex: "+this.radDropDownList1.SelectedIndex);
}
public class Item
{
public int ItemCode { get; set; }
public string Description { get; set; }
public Item(int itemCode, string description)
{
this.ItemCode = itemCode;
this.Description = description;
}
}
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Progress is here for your business, like always.
Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.