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

Setting DropDownList SelectedItem

25 Answers 2241 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
SteveL
Top achievements
Rank 2
SteveL asked on 21 Jul 2010, 08:27 PM
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

25 Answers, 1 is accepted

Sort by
0
Ramius
Top achievements
Rank 1
answered on 22 Jul 2010, 12:29 AM

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

0
SteveL
Top achievements
Rank 2
answered on 22 Jul 2010, 10:19 AM
Thank you, Ramius, I knew it was going to be something simple.

Steve
0
Accepted
Victor
Telerik team
answered on 22 Jul 2010, 11:55 AM
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
0
Ramius
Top achievements
Rank 1
answered on 22 Jul 2010, 09:56 PM
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
0
Victor
Telerik team
answered on 26 Jul 2010, 05:37 PM
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
0
Adrian
Top achievements
Rank 2
answered on 10 Aug 2010, 12:59 PM
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.
0
Adrian
Top achievements
Rank 2
answered on 10 Aug 2010, 01:22 PM
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" ...

 

 

 

 

0
Peter
Telerik team
answered on 13 Aug 2010, 10:14 AM
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
0
SteveL
Top achievements
Rank 2
answered on 16 Aug 2010, 09:18 PM
It appears that FindString() does a 'starts with' search. How do I get it to do an exact match search, please?

Steve
0
SteveL
Top achievements
Rank 2
answered on 19 Aug 2010, 07:52 AM
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
0
Peter
Telerik team
answered on 19 Aug 2010, 07:57 AM
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
0
Carol Watson
Top achievements
Rank 1
answered on 29 Sep 2010, 03:49 AM
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...
0
Peter
Telerik team
answered on 01 Oct 2010, 01:29 PM
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
0
Aravind
Top achievements
Rank 1
answered on 12 Jan 2013, 09:42 AM
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...

0
Peter
Telerik team
answered on 14 Jan 2013, 02:50 PM
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.
0
Wesley
Top achievements
Rank 1
answered on 19 May 2015, 10:50 AM

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?



0
Wesley
Top achievements
Rank 1
answered on 19 May 2015, 10:51 AM
ddl.Count() is equal to 1.
0
Dimitar
Telerik team
answered on 20 May 2015, 08:58 AM
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
0
Les
Top achievements
Rank 1
answered on 20 Jan 2016, 03:59 PM
Any news on implementing RadDropDownList. SelectedValue ?
0
Dimitar
Telerik team
answered on 21 Jan 2016, 10:42 AM
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
0
Vitaly
Top achievements
Rank 1
answered on 05 Apr 2016, 10:11 AM

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;
}

0
Dimitar
Telerik team
answered on 05 Apr 2016, 11:20 AM
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.
0
Vitaly
Top achievements
Rank 1
answered on 05 Apr 2016, 11:32 AM

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

Anyway, thanks for the answer!

1
Dimitar
Telerik team
answered on 06 Apr 2016, 12:38 PM
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 .

At the same 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.
0
Vitaly
Top achievements
Rank 1
answered on 07 Apr 2016, 06:52 AM

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.

Tags
DropDownList
Asked by
SteveL
Top achievements
Rank 2
Answers by
Ramius
Top achievements
Rank 1
SteveL
Top achievements
Rank 2
Victor
Telerik team
Adrian
Top achievements
Rank 2
Peter
Telerik team
Carol Watson
Top achievements
Rank 1
Aravind
Top achievements
Rank 1
Wesley
Top achievements
Rank 1
Dimitar
Telerik team
Les
Top achievements
Rank 1
Vitaly
Top achievements
Rank 1
Share this question
or