25 Answers, 1 is accepted
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
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
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
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
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.
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" ...
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
Steve
Steve
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
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...
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
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...
Thank you for contacting Telerik support.
You should set the RadDropDownList's DropDownStyle property to DropDownList. This 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
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?
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
RadDropDownList already has SelectedValue property. More information is available here: Selection.
Let me know if I can assist you further.
Regards,
Dimitar
Telerik
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;
}
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
Oh, so it is the old bug.. since 2013, really? And nobody care? It's disappointing somehow...
Anyway, thanks for the answer!
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
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.