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

Insert default item in dropdownlist

28 Answers 1804 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Vaibhav
Top achievements
Rank 1
Vaibhav asked on 17 Sep 2010, 06:43 AM
Hi,
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

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 22 Sep 2010, 07:35 AM
Hello Vaibhav,

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
0
jfkrueger
Top achievements
Rank 1
answered on 12 Oct 2010, 11:17 PM
Adding a default value to a drop-down list goes against best practices & UI standards? Care to elaborate on that since it is easily done with the ASP.NET Ajax controls and is contained in virtually every drop down list you find anywhere (winforms and web).
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 04:24 AM
Hello jfkrueger,

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


0
jfkrueger
Top achievements
Rank 1
answered on 13 Oct 2010, 02:07 PM
Ummm...yes I am well aware of the many differences between windows forms and web, but thanks! The fact is, a dropdown list is a dropdown list no matter where it is, winforms, web etc. and it is NOT against UI standards and best practices to have a default item such as "Select A Value" as the first item in the list. That is a ridiculous statement in my humble opinion since having a Default (NOT Null) value only enhances usability and actually makes the control able to be validated (otherwise the user can just leave the first item selected and you can't validate that they actually made a selection vs. forgetting and leaving the 1st item selected). But hey, that's just my opinion and as you say we are all entitled to our own opinions. Just wanted to throw my two cents in.

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.

0
Richard Slade
Top achievements
Rank 2
answered on 13 Oct 2010, 02:43 PM
Hello, 

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
0
jfkrueger
Top achievements
Rank 1
answered on 13 Oct 2010, 02:53 PM
Well then your method that returns the datasource should have a parameter called "IncludeDefaultOption" or something like that, or you could have seperate methods entirely or you can add it AFTER the method returns the actual data. This doesn't break anything for anyone else. NOT having a default "Select Option" is against best practice and UI design in my opinion - how you get it there is up to you and yes some of the ways you may choose to do this may not be the best. I just took issue with someone from Telerik basically saying that having a "Select Option" is against best practices and UI design, that is not the case IMO. The best way to do this would be exactly how it is done in the web. Have an OnDataBound event for the dropdown and add the item in code there - no messing with the underlying data. The ONLY reason you have to mess with the underlying data in winforms is because you are not able to add the item programatically AND bind the list (in a smart way).

Once again, just my opinion.
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 03:33 PM
Hello again,

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
0
jfkrueger
Top achievements
Rank 1
answered on 13 Oct 2010, 03:51 PM
Hey as long as the option is in the list I could care less where it comes from or what it is. The original response from Telerik was unclear and basically told the guy that it is stupid to put a default option in the list - he didn't specify that it was only the way he was doing it that was incorrect. If you are able to have a "Select Option" or whatever as the first item in the list so that you can validate that a user actually made a selection, that's all I am talking about. So I am obviously not aware of this "Null Text" but I will look into it because adding a value to a datasource IS dumb, but seemed to be the easiest way. All I am saying is that it is NOT bad design to have a default option and that it IS bad design to NOT have a default option. Why don't you show me an application anywhere in which a user is required to select a value from a drop-down list that does NOT have a default option. If you can, it is a poorly designed app. Once again, not talking about how it is implemented I'm just talking about simply having a default item which is what the original response seemed to be saying was a bad idea.
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Oct 2010, 04:24 PM
Hello again,

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
0
Peter
Telerik team
answered on 15 Oct 2010, 04:58 PM
Hi Vaibhav,

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
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
Chad Hensley
Top achievements
Rank 1
answered on 26 Feb 2011, 09:34 AM
Hello,

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. 
0
Richard Slade
Top achievements
Rank 2
answered on 26 Feb 2011, 11:15 AM
Hello,

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
0
Chad Hensley
Top achievements
Rank 1
answered on 26 Feb 2011, 11:16 AM
I went with the unbound method. 
0
jfkrueger
Top achievements
Rank 1
answered on 23 Mar 2011, 07:34 PM
Just for the record, setting "NullText" on a DropDownList in which you bind a dataset/table to does not work. The text you enter is not an option in the list (Please select an option) and the first item in the dataset is the first item in the list, which brings us back to having to add a record to the dataset at some point in order to accomplish this functionality that should be intrinsic. So not sure what people are saying about the "NullText" property but it seems to be useless if you are actually binding any data to the control.
0
Richard Slade
Top achievements
Rank 2
answered on 23 Mar 2011, 10:52 PM
Hello jfkrueger,

The NullText property is there for when you have the DropDownStyle set to
this.radDropDownList1.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
and is not used in DropDownList style. It is also intended to give some text when the text is null, and isn't a list option (as in Please Select an Option).

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
0
jfkrueger
Top achievements
Rank 1
answered on 24 Mar 2011, 05:04 PM
Hmm well that is what the DropDownStyle is set to and the NullText works great...until you bind any data to the list. So if you are telling me that you can bind a dataset to the RadDropDownList AND have the NullText property work (by displaying what is in the NullText property as the selected item when the form initially loads) then I would have to disagree - or at least say that it sure does not work that way in my app. Can you show me an example where you are binding a dataset/datatable to the dropdownlist and also have a default "Please make a selection" item in the list (without adding it to the underlying dataset somehow)?

Thanks!
0
Richard Slade
Top achievements
Rank 2
answered on 24 Mar 2011, 10:05 PM
Hello,

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
0
jfkrueger
Top achievements
Rank 1
answered on 24 Mar 2011, 10:07 PM
Lol, yes this is the point I have been trying to make since my original post on here. Thanks!
0
Richard Slade
Top achievements
Rank 2
answered on 24 Mar 2011, 10:21 PM
Hello,

For information, this is the same as the Microsoft standard. I.e It works the same way with aa ComboBox
Regards,
Richard
0
Peter
Telerik team
answered on 28 Mar 2011, 06:14 PM
Hello Joe,

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.

Best wishes,
Peter
the Telerik team
0
jfkrueger
Top achievements
Rank 1
answered on 29 Mar 2011, 04:37 PM
I did see a reference to this earlier in the topic so I tried it....it didn't work. I'll take a look at your example though and give it another shot. Thanks!
0
Dave
Top achievements
Rank 1
answered on 31 May 2011, 04:34 PM
You can change the underlying data source for your control by using an SQL union query. See blog below http://www.wiseowl.co.uk/blog/s110/set-first-item-list-control.htm
0
jfkrueger
Top achievements
Rank 1
answered on 27 Jun 2011, 08:44 PM
Okay, manually setting the selectedindex to -1 seems to do the trick so that definitely fills the need. Excellent, thank you!
0
James
Top achievements
Rank 1
answered on 13 Nov 2012, 06:04 PM
<admin>sarcasm</admin>. It's like there is some bible of coding practices that every programmer "should have read". The book was blessed next to you at birth when it was ordained you would program some day. You should know that... lol <admin>offensive language</admin>. Krueger asked a simple question. Awnsers it or just don't respond. 
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Nov 2012, 06:14 PM
Thank you James, <admin>sarcasm</admin>

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
0
James
Top achievements
Rank 1
answered on 13 Nov 2012, 06:18 PM
#1 Passive Aggressive - look it up dude. Are you really thanking me? 
#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>
0
Emanuel Varga
Top achievements
Rank 1
answered on 13 Nov 2012, 06:42 PM
Everyone has the right to freedom of opinion and expression.

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.
0
Nikolay
Telerik team
answered on 15 Nov 2012, 02:25 PM
Hi guys,

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.

All the best,
Nikolay
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
DropDownList
Asked by
Vaibhav
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
jfkrueger
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Peter
Telerik team
Chad Hensley
Top achievements
Rank 1
Dave
Top achievements
Rank 1
James
Top achievements
Rank 1
Nikolay
Telerik team
Share this question
or