Setting DropDownList SelectedItem

26 posts, 1 answers
  1. SteveL
    SteveL avatar
    68 posts
    Member since:
    Apr 2009

    Posted 21 Jul 2010 Link to this post

    How do I set the selected item (without using the index) from code? With combo box I just set SelectedItem to a string containing one of the items. I don't seem to be able to do that with DropDownList?

    Steve
  2. Ramius
    Ramius avatar
    127 posts
    Member since:
    Feb 2008

    Posted 21 Jul 2010 Link to this post

    Hello Steve,

    it is very similar to the combobox, but the DropDownList.SelectedItem expects a RadListDataItem.

    The RadListDataItem has a Text property.

    In Code:

    RadListDataItem Item  = new RadListDataItem();

    Item.Text = "my string";

    this.DropDownList1.SelectedItem = Item;

    Regards,

    Ramius

  3. UI for WinForms is Visual Studio 2017 Ready
  4. SteveL
    SteveL avatar
    68 posts
    Member since:
    Apr 2009

    Posted 22 Jul 2010 Link to this post

    Thank you, Ramius, I knew it was going to be something simple.

    Steve
  5. Answer
    Victor
    Admin
    Victor avatar
    1351 posts

    Posted 22 Jul 2010 Link to this post

    Hi Steve,

    Thank you for writing.

    Currently if you use the code that Ramius provided you will notice that an InvalidOperationException is thrown. This is because when an item is created with the new operator, it has not yet been added to the  Items collection and its Owner property will return null. When you set the SelectedItem property and if the value's Owner is different than the drop down list element's, it will throw an exception.


    You can select an item by string by calling the FinsString() method with the desired string, then assign the result to SelectedIndex (FindString() returns an int). In order to avoid writing this code every time you can abstract it away in an extension method if you are using .NET Framework 3.0 or later. Here is how your code could look like:

    Define the extender class

    public static class RadDropDownListExtender
    {
        public static void SelectByText(this RadDropDownList dropDown, string searchItem)
        {
            dropDown.SelectedIndex = dropDown.FindString(searchItem);
        }
    }

    Use it wherever you need
     
    this.radDropDownList1.SelectByText("MyItem");
     
    We will consider adding a property to RadDropDownList so that it support this functionality out of the box.
    Please write again if you have other questions.

    Greetings,
    Victor
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Ramius
    Ramius avatar
    127 posts
    Member since:
    Feb 2008

    Posted 22 Jul 2010 Link to this post

    Hello Victor,

    thank you for your explanation. Your are right, my code is not working.
    I have a similar code working, but before i set the SelectedItem property i add the item to the RadDropDownList.

    Greetings,

    Ramius
  7. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 26 Jul 2010 Link to this post

    Hello Ramius,

    I am glad to hear that the issue is resolved. I am ready to assist you should you guys have more questions.
     
    Regards,
    Victor
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Adrian
    Adrian avatar
    14 posts
    Member since:
    Apr 2010

    Posted 10 Aug 2010 Link to this post

    Hi.

    Am I right that DropDownList is not offering any set of SelectedValue by Design time anymore.

    I switched my first Form now from RadComboBox to RadDropDownList and I am missing a lot of features.

    Let me describe:

    We have a list of objects e.g. the Language, meaning Language_ID and Language_Name.

    So the RadComboBox DataSource was the BindingSource filled with a list of Language Objects.

    On the other hand we have the so called "mother" object which is e.g. the Customer Object. The customer object is also
    bound to a BindingSource. So what was really nice fancy behavior in RadComboBox i was able to set the SelectedValue
    property in DataBinding to "customerBindingSource.Language", meaning if the customerObject was filled and the Language
    was already set the corresponding RadComboBox with a List of all possible Languages was correctly set.
    Also a change of the Language was directly affecting the Object property change of customer.language.

    Please tell me if i had overseen something or this is features are not part of the DropDownList control anymore.

    Think of we have a lot of controls, of course also a lot of RadComboBox controls. Changing those to DropDownList
    not having above mentioned functionality will cause a lot of workload.

    Thanks in advance.

    All the best.

    Adrian.
  9. Adrian
    Adrian avatar
    14 posts
    Member since:
    Apr 2010

    Posted 10 Aug 2010 Link to this post

    so what i found out is that "SelectedValue" for new RadDropDownList is still available, but only after i manually code e.g. following line in the *.Designer.cs file (like it looks like in RadComboBox.

    this.Language_ComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.CustomerBindingSource, "Language", true));

    After i did this, also the VisualStudio Designer IDE shows me for this special DropDownList the "SelectedValue" property under "DataBindings" ...

     

     

     

     

  10. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 13 Aug 2010 Link to this post

    Hi Adrian,

    Thank you for the request.

    Actually, our team plans to introduce this functionality in our next release.

    Do not hesitate to contact us if you have other questions.

    Greetings,
    Peter
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. SteveL
    SteveL avatar
    68 posts
    Member since:
    Apr 2009

    Posted 16 Aug 2010 Link to this post

    It appears that FindString() does a 'starts with' search. How do I get it to do an exact match search, please?

    Steve
  12. SteveL
    SteveL avatar
    68 posts
    Member since:
    Apr 2009

    Posted 19 Aug 2010 Link to this post

    If there isn't a solution to this, is there any way I can convert my controls back into Combo Boxes so that they work again, now that they have disappeared from the toolbox?

    Steve
  13. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 19 Aug 2010 Link to this post

    Hi SteveL,

    Thanks for getting back to me.

    You should use ListElement.FindStringExact method. Here is an example:

    this.Language_ComboBox.ListElement.FindStringExact

    I hope this helps. Let me know if you have further questions.

    Sincerely yours,
    Peter
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. Carol Watson
    Carol Watson avatar
    21 posts
    Member since:
    Jan 2010

    Posted 28 Sep 2010 Link to this post

    Hi Adrian... I have the same problem you have with the loss of the "selectedvalue" property.  I tried your solution and found that I don't get the expected result.
    My "Quick and dirty " workaround was to create a routine that would check find the value for the "SelectedValue" in the DropdownList Items and set the item as selected if it found it.  I did this check for each record

    I hope there is a fix for this and soon.  The form I have tried this with has 4 DropDownList controls... Will be lots more work if there more forms and lists...
  15. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 01 Oct 2010 Link to this post

    Hello Carol Watson,

    Thanks for the writing.
     
    This feature was introduced in our SP2 (2010.2.10 914) release.
     
    Do not hesitate to contact us if you have other questions.

    Kind regards,
    Peter
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  16. Aravind
    Aravind avatar
    3 posts
    Member since:
    Nov 2012

    Posted 12 Jan 2013 Link to this post

    hi
       i am in demo version so i cant open new ticket,

     how to select only value from rad dropdownlist ,i.e i want select only value and not to write any character in the dropdownlist,
    bcz i want insert value into databse for dropdownlist selecteitem,if i select vaule from dropdownlist and add some letters with that value ,that value also inserted into database,
    so help me which property to disable the typing letters into dropdownlist.


    thank you...

  17. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 14 Jan 2013 Link to this post

    Hello Aravind,

    Thank you for contacting Telerik support.

    You should set the RadDropDownList's 
    DropDownStyle property to DropDownListThis property determines if the text area at the top of the control can be edited. A setting of DropDown (the default) allows editing and the DropDownList setting shows the text area as read-only.

    I hope this helps.

    Kind regards,
    Peter
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  18. Wesley
    Wesley avatar
    2 posts
    Member since:
    May 2015

    Posted 19 May 2015 in reply to Peter Link to this post

    I tried this: 

        public partial class MyDropDownList : RadDropDownList

        {

    ...

            public void SelectByText( string searchItem)
            {
                RadDropDownListExtender.SelectByText(this, searchItem);
            }
        }

        public static class RadDropDownListExtender
        {
            public static void SelectByText(this RadDropDownList dropDown, string searchItem)
            {
                dropDown.SelectedIndex = dropDown.FindString(searchItem);
                // dropDown.SelectedText = searchItem;
            }
        }

    ... and in my dialog form:

                    RadListDataItem item = new RadListDataItem();
                    item = ddl.Items.First();

                    ddl.SelectByText(item.Text);

    and the selected text stays empty. What do I wrong?



  19. Wesley
    Wesley avatar
    2 posts
    Member since:
    May 2015

    Posted 19 May 2015 in reply to Wesley Link to this post

    ddl.Count() is equal to 1.
  20. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 20 May 2015 Link to this post

    Hello Wesley,

    Thank you for writing.

    I have tested this and on my side your code is working properly. For convenience I prepared a small sample, based on the information that you provided so far and attached it to this thread. Could you please check it and let me know how it differs from your real setup? 

    Additionally, I wanted to ask you what is the exact version of the suite that you are currently using?
     
    I am looking forward to your reply.

    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  21. Les
    Les avatar
    1 posts
    Member since:
    Mar 2012

    Posted 20 Jan Link to this post

    Any news on implementing RadDropDownList. SelectedValue ?
  22. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 21 Jan Link to this post

    Hi Les,

    RadDropDownList already has SelectedValue property. More information is available here: Selection.

    Let me know if I can assist you further.

    Regards,
    Dimitar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  23. Vitaly
    Vitaly avatar
    8 posts
    Member since:
    Mar 2015

    Posted 05 Apr Link to this post

    Hi.

    I have some bug with SelectedItem binding in 2016.1.216 version - value of bound object is not changing, when SelectedIndex changed. Initial value is displayed correctly.

    Here is simple example.

    public enum State
    {
        Work = 0,
        Idle = 1,
        Maintenance = 2
    }
     
    public class Entity
    {
        public int Id { get; set; }
     
        public State State { get; set; }
    }

    public partial class Form1 : RadForm
    {
        private Entity _entity;
     
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
             
            _entity = new Entity {Id = 1, State = State.Idle};
            bindingSource.DataSource = _entity;
        }
     
        private void radButton1_Click(object sender, System.EventArgs e)
        {
            if ((State) radDropDownList1.SelectedIndex != _entity.State)
                RadMessageBox.Show(string.Format("Selected index is {0}, but entity State value is {1}.\nWhy?",
                    radDropDownList1.SelectedIndex, (int) _entity.State), "Error");
        }
    }

    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
     
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
     
        #region Windows Form Designer generated code
     
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            Telerik.WinControls.UI.RadListDataItem radListDataItem1 = new Telerik.WinControls.UI.RadListDataItem();
            Telerik.WinControls.UI.RadListDataItem radListDataItem2 = new Telerik.WinControls.UI.RadListDataItem();
            Telerik.WinControls.UI.RadListDataItem radListDataItem3 = new Telerik.WinControls.UI.RadListDataItem();
            this.radSpinEditor1 = new Telerik.WinControls.UI.RadSpinEditor();
            this.radLabel1 = new Telerik.WinControls.UI.RadLabel();
            this.radLabel2 = new Telerik.WinControls.UI.RadLabel();
            this.radDropDownList1 = new Telerik.WinControls.UI.RadDropDownList();
            this.radButton1 = new Telerik.WinControls.UI.RadButton();
            this.bindingSource = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.radSpinEditor1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radLabel1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radLabel2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radDropDownList1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            //
            // radSpinEditor1
            //
            this.radSpinEditor1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource, "Id", true));
            this.radSpinEditor1.Location = new System.Drawing.Point(63, 13);
            this.radSpinEditor1.Name = "radSpinEditor1";
            this.radSpinEditor1.Size = new System.Drawing.Size(100, 20);
            this.radSpinEditor1.TabIndex = 0;
            this.radSpinEditor1.TabStop = false;
            //
            // radLabel1
            //
            this.radLabel1.Location = new System.Drawing.Point(12, 14);
            this.radLabel1.Name = "radLabel1";
            this.radLabel1.Size = new System.Drawing.Size(16, 18);
            this.radLabel1.TabIndex = 1;
            this.radLabel1.Text = "Id";
            //
            // radLabel2
            //
            this.radLabel2.Location = new System.Drawing.Point(12, 54);
            this.radLabel2.Name = "radLabel2";
            this.radLabel2.Size = new System.Drawing.Size(32, 18);
            this.radLabel2.TabIndex = 2;
            this.radLabel2.Text = "State";
            //
            // radDropDownList1
            //
            this.radDropDownList1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedIndex", this.bindingSource, "State", true));
            this.radDropDownList1.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
            radListDataItem1.Text = "Work";
            radListDataItem2.Text = "Idle";
            radListDataItem3.Text = "Maintenance";
            this.radDropDownList1.Items.Add(radListDataItem1);
            this.radDropDownList1.Items.Add(radListDataItem2);
            this.radDropDownList1.Items.Add(radListDataItem3);
            this.radDropDownList1.Location = new System.Drawing.Point(63, 52);
            this.radDropDownList1.Name = "radDropDownList1";
            this.radDropDownList1.Size = new System.Drawing.Size(125, 20);
            this.radDropDownList1.TabIndex = 3;
            //
            // radButton1
            //
            this.radButton1.Location = new System.Drawing.Point(170, 114);
            this.radButton1.Name = "radButton1";
            this.radButton1.Size = new System.Drawing.Size(110, 24);
            this.radButton1.TabIndex = 4;
            this.radButton1.Text = "Save";
            this.radButton1.Click += new System.EventHandler(this.radButton1_Click);
            //
            // bindingSource
            //
            this.bindingSource.DataSource = typeof(DropDownListBinding.Entity);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 144);
            this.Controls.Add(this.radButton1);
            this.Controls.Add(this.radDropDownList1);
            this.Controls.Add(this.radLabel2);
            this.Controls.Add(this.radLabel1);
            this.Controls.Add(this.radSpinEditor1);
            this.Name = "Form1";
            //
            //
            //
            this.RootElement.ApplyShapeToControl = true;
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.radSpinEditor1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radLabel1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radLabel2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radDropDownList1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radButton1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
     
        }
     
        #endregion
     
        private Telerik.WinControls.UI.RadSpinEditor radSpinEditor1;
        private Telerik.WinControls.UI.RadLabel radLabel1;
        private System.Windows.Forms.BindingSource bindingSource;
        private Telerik.WinControls.UI.RadLabel radLabel2;
        private Telerik.WinControls.UI.RadDropDownList radDropDownList1;
        private Telerik.WinControls.UI.RadButton radButton1;
    }

  24. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 05 Apr Link to this post

    Hi Vitaly,

    Thank you for writing.

    This is a known issue which is already logged in our Feedback Portal. You can track the item for status changes and add your vote for it here.

    The item description contains a workaround that you can use until this is resolved. 

    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  25. Vitaly
    Vitaly avatar
    8 posts
    Member since:
    Mar 2015

    Posted 05 Apr Link to this post

    Oh, so it is the old bug.. since 2013, really? And nobody care? It's disappointing somehow...

    Anyway, thanks for the answer!

  26. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 06 Apr Link to this post

    Hello Vitaly,

    Thank you for writing back.

    It is not that we don't care. The case is that fixing the issue requires introducing a
    breaking change that would affect a lot of our customers projects and if we are going to introduce this change, we would like to see high demand for it. The feedback item was updated with this information to make it more clear.

    At the same time there is an easy workaround which you can use.

    Thank you for your understanding.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  27. Vitaly
    Vitaly avatar
    8 posts
    Member since:
    Mar 2015

    Posted 07 Apr in reply to Dimitar Link to this post

    Hello!

    So, broken binding, in that case, is not a bug anymore.

    It is the Feature! :)

    Maybe, it has to be described accordingly in official documentation?

    P.S. Nah, just kidding.

Back to Top
UI for WinForms is Visual Studio 2017 Ready