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

Problem with selectedvalue

2 Answers 198 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 01 Apr 2020, 12:56 PM

Hello

I have a Droddownlist that is populated with a datasource that contains a liste of objects that has 5 properties; I set thedisplaymember and valuemember to two properties ("Description" and "ItemCode") of the object.

Immediately after assigning the datasource a "selectedIndexChanged" event is fired; at this point I have in the selectedvalue property the object of index 0 of the datasource, therefore I can access to the valuemember by "selectedValue.ItemCode"

When, later, the user selects an item in the dropdownliste, the selectedindex is fired, the "selectedItemValue" contains the property "itemcode" of the selected item and not the selected object

Consequently I dont know how to get access to the "itemCode" property in the selectedIndex event, if I use .SelectedValue.itemcode it works after the loading but fails when the user selects an item; and if I use SelectedValue the code fails immediately after databinding

Thanks in advance for your advice

Pierre-Jean

 

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Apr 2020, 07:35 AM
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.
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 06 Apr 2020, 01:10 PM

Hello Nadya

Thanks a lot

it works perfectly 

Thanks again

PJ

Tags
DropDownList
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or