I am using Telerik.WinControls.UI,RadDropdownList in winform.I have to display user list into dropdown.but prior to that i want to add "select user" as default item in dropdown list.
When I tried it using following code,it gave me following error.
Display:
I have datasource that contains John,Andy and Ren as users.While displaying it on winform,I want to display "--select user--" as first item in it. like below;
I want following type of dropnlist
--select user--
John
Andy
Ren
Code is given below:
Dim uBl As New UserBusinessLogic()
Dim _user As IQueryable(Of User) = uBl.GetAllUsersByCompany(_project.Company.CompanyID)
Dim RadListDataItem1 As RadListDataItem = New RadListDataItem()
RadListDataItem1.Selected = True
RadListDataItem1.Text = "Select User"
RadListDataItem1.Value = "0"
Me.ddlUsers.DataSource = Nothing
ddlUsers.Items.Clear()
Me.ddlUsers.DataSource = _user
Me.ddlUsers.DisplayMember = "UserName"
Me.ddlUsers.ValueMember = "UserID"
Me.ddlUsers.Items.Add(RadListDataItem1)
Error:
RadListControl can not transition directly from bound to unbound mode. The data source must be removed first.
please give me solution ASAP.
Thanks in advance.
Vaibhav
28 Answers, 1 is accepted
First of all you shouldn't do things like this because this goes against more than a few UI best practices & conventions.
But if you really want to i would suggest going to unbound mode and create all of the users from your data source as items for the list and add them like that, but this in turn would be memory and time consuming, but it will work.
//.. or, on the easy but stupid side, you could just create a dummy object for your collection and insert it at position 0.
Hope this helps,
Best Regards,
Emanuel Varga
You are right about that, but that is the web, and this is windows forms, and don't you think that there is a reason why that is that easily done in asp.net and not here? There should be, and always will be differences between the behavior that you see and should see on the web and on the one you see in windows forms, at least in my point of view.
In my point of view, a dropdown should not contain a null element, the null text is more than enough, if you don't want to accept nulls for that element, other than that why would you let the user revert to the null element?
But like always, it's a matter of design and preferences.
Best Regards,
Emanuel Varga
And to the original poster the only way I have found to do this in WinForms is to actually add the value to the datasource of the DropDownList.
In my opinion, the two ways to achieve this are the 2 ways described by Emanuel (with a preference for unbound mode),
I also have to agree with Emanuel that it is not (generally) a good idea to add a default dummy item to a datasource as (for one reason...) this may negate the possibility of re-using this datasource for other uses.
Public
Class
Form1
Private
Sub
Form1_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
Dim
Cars
As
New
System.Collections.Generic.List(Of Car)
Cars.Add(
New
Car(
"BMW"
, 1))
Cars.Add(
New
Car(
"Audi"
, 2))
Cars.Add(
New
Car(
"Ford"
, 3))
'Me.RadDropDownList1.DataSource = Cars
'Me.RadDropDownList1.DisplayMember = "Name"
'Me.RadDropDownList1.ValueMember = "Id"
For
Each
c
As
Car
In
Cars
Dim
item
As
New
RadListDataItem()
item.Value = c.Id
item.Text = c.Name
Me
.RadDropDownList1.Items.Add(item)
Next
Dim
defaultItem
As
New
RadListDataItem()
defaultItem.Text =
"Select a value"
defaultItem.Value = 0
Me
.RadDropDownList1.Items.Insert(0, defaultItem)
Me
.RadDropDownList1.SelectedIndex = 0
End
Sub
End
Class
Public
Class
Car
Private
m_Name
As
String
Private
m_Id
As
Integer
Public
Sub
New
(
ByVal
Name
As
String
,
ByVal
Id
As
Integer
)
m_Name = Name
m_Id = Id
End
Sub
Public
ReadOnly
Property
Name()
As
String
Get
Return
m_Name
End
Get
End
Property
Public
ReadOnly
Property
Id()
As
Integer
Get
Return
m_Id
End
Get
End
Property
End
Class
All the best
Richard
Once again, just my opinion.
I have to tell you again, in my point of view, you don't need an actual item in the list, it is enough to have a NullText, to say select something when no item is selected, so when the user opens the drop down he will see just the available items, not the Select Item Item + all the items.
For validation, what can be easier than to check if SelectedIndex is not -1 or SelectedItem is not null and so on?
Another good thing about using Null Text, is that once the user selected something, he cannot go back to Select Item, what would be the point of that?
Please take a look at the DropDown with Null text inside the telerik control suite and you will see what i mean. The only problem is The null text is not shown when the dropdown is in List mode, (this i still don't know why).
And again, i have to agree to disagree, but i would like to ask you to give me one example in the whole windows operating system where they made a dropdown with a "Please Select" text and you can select it, deselect it, select it again, but it will fail validation.
Best Regards,
Emanuel Varga
The original answer was from me, not from telerik, and i just told him that he shouldn't do things like this as for the exception he received, and after that i offered two different solutions?
If i did something wrong i apologize, i was just offering my opinion, i cannot and will not tell anybody what to do.
From what I've been seeing here you were telling the same thing, to add the dummy item either to the collection or to the items of the dropdown.
Anyway in my point of view it would have been great if Telerik would have added a NullItem property and that item to be selected when the control is loaded, NOT the first item in the collection ( when using a data source.)
Best Regards,
Emanuel Varga
Thank you for contacting us.
The error message "cannot transition directly from bound to unbound mode" means that you have data bound RadListControl/RadDropDownList. When using the control in data bound mode you should not manipulate the items directly (e.g. you should not call the Insert , Add or Clear methods of the Items collection):
ddlUsers.Items.Clear()
....
ddlUsers.Items.Add(RadListDataItem1)
I agree with Emanuel and Richard. It will be enough to set the NullText property.
I hope this helps. If you have other questions, please feel free to write back.
Regards,
Peter
the Telerik team
It may go against "best practices", but a "default value" also allows the user to choose the option they want.
I.e. I have a grid that is populated based on the dropdownlist selection. This grid can have thousands and thousands of records. It loads every single time the form opens because the dropdownlist is always pointed to the first record, instead of a blank record and selectedindex = -1.
You can put in the NullText and SelectedIndex=-1 etc., but as soon as you bind it to a datatable it sets the selectedindex to 0 and the first item, which kicks off the population of the grid.
The user is more important to me than "best practices". The user loses in this case.
Putting the selected index to -1 is good.
You just need to add the event handler after binding. Then the selected index change won't fire until the user picks a value.
Regards,
Richard
The NullText property is there for when you have the DropDownStyle set to
this
.radDropDownList1.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
The first item in the list will be selected by design. If you wish to not have an option selected, then set the SelectedIndex property to -1 as per my previous post. As per other posts, the SelectedIndex = -1 can easily be validated.
I hope that helps.
Richard
Thanks!
You cannot add unbound items to a bound control. You would need to either add your extra item to the bound collection or add all unbound items.
Regards,
Richard
For information, this is the same as the Microsoft standard. I.e It works the same way with aa ComboBox
Regards,
Richard
By default, RadDataSource causes RadListControl and RadDropDownList to select the first item, e.g. it sets the current position to 0 and this is a desired behavior.
You should manually set SelectedIndex to -1 after the form is loaded if you want to remove the selection and see the Null Text. You have to bear in the mind that you can see Null text only in case that the control is not in focus (this is a desired behavior).
Please refer to the attached project and to the attached movie.
I hope this helps.
Peter
the Telerik team
Maybe you should have some consideration for the people who are actually trying to help and give something back to the community.
P.s. Krugger did not ask the question, the thread was created by Vaibhav, krugger was just the first james to reply on this thread.
Best regards,
Emanuel Varga
WinForms MVP
#2 Consideration - Maybe you should have some consideration for the assistance needed instead of lecturing people about imaginary standards.
#3 Doesn't matter who asked it. Off topic. lol
<admin>offensive language</admin>
You can either take it into consideration or not.
Other than that, like i said before i am just trying to help and i believe I was not the one who made offensive comments.
Please try to stay close to the main topic of the thread using business tone of language only. Direct offensive language as well as sarcasm and irony are not tolerated.
@James: a part of your comments was deleted due to the use of offensive language. Please try to keep your comments close to the main topic of the thread, and if you have comments concerning a specific answer of somebody else, as I mentioned above, keep close to the business tone of language and express your feedback in a constructive way. Thank you for your understanding.
Nikolay
the Telerik team