Problem with selectedvalue

3 posts, 0 answers
  1. pierre-jean
    pierre-jean avatar
    162 posts
    Member since:
    Apr 2011

    Posted 01 Apr 2020 Link to this post

    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. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    4174 posts

    Posted 06 Apr 2020 Link to this post

    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.
  3. pierre-jean
    pierre-jean avatar
    162 posts
    Member since:
    Apr 2011

    Posted 06 Apr 2020 in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hello Nadya

    Thanks a lot

    it works perfectly 

    Thanks again

    PJ

Back to Top