Adding a 'Choose One' item to combo boxAdding a 'Choose One' item to combo box
Old forum URL: forums.lhotka.net/forums/t/4144.aspx
JonM posted on Thursday, January 10, 2008
In a search window I have a read-only collection of possible types to be used as a paramter in my search. However, I want to add an extra item to the combo like 'Choose One'. If set to 'Choose One' it would actually be null and I would know not to use this parameter in the search. So far the only way I've found to achieve this is to muck up my read only collection in order to get it to add an extra. Any ideas on this one? It seems to me like it would be a common thing you would want to do.
JoeFallon1 replied on Thursday, January 10, 2008
This has been discussed many times. There are some interesting solutions posted too. You should be able to find them with a Search.
As I recall there is one very nice solution which abstracts this whole idea nd lets you add whatever the phrase of the day is - "Choose One", "blank", "Add New", "Pick anything but me", etc.
I usually have 2 methods on my NVL which tell me to retrieve the data or add the "fake first row" and then then retrieve the data.
Joe
JohnB replied on Thursday, January 10, 2008
Here is one method of doing it.
Let's assume that we wish to fill a combo with customers so I would have the following:
<Serializable()> _
Public Class CustomerComboList
Inherits Csla.ReadOnlyListBase(Of CustomerComboList, CustomerComboInfo)
Public Shared Function GetCustomerComboList(Optional ByVal FirstItemInList As String = "") As CustomerComboList
Return Csla.DataPortal.Fetch(Of CustomerComboList)(New Criteria(FirstItemInList))
End Function
In my Criteria object for the CustomerComboList I would have the following:
Private Class Criteria
Private _firstItemInList As String = String.Empty
Public ReadOnly Property FirstItemInList() As String
Get
Return _firstItemInList
End Get
End Property
Public Sub New(ByVal FirstItemInList As String)
_firstItemInList = FirstItemInList
End Sub
End Class
Finally the Fetch:
Private Sub Fetch(ByVal criteria As Criteria)
RaiseListChangedEvents = False
Using cn As New SqlConnection(MyConfig.DataConnection)
cn.Open()
Using cm As SqlCommand = cn.CreateCommand
With cm
.CommandType = CommandType.StoredProcedure
.CommandText = "SomeProcName"
Using dr As New Csla.Data.SafeDataReader(.ExecuteReader)
IsReadOnly = False
'-- Check to see if we need to add a custom first item to the list
If criteria.FirstItemInList.Length > 0 Then
Dim firstItem As CustomerComboInfo = New LocationInfo(-1, criteria.FirstItemInList)
Me.Add(firstItem)
End If
While dr.Read()
Dim info As New CustomerComboInfo(dr.GetInt32(0), dr.GetString(1), dr.GetInt32(2))
Me.Add(info)
End While
IsReadOnly = True
End Using
End With
End Using
End Using
RaiseListChangedEvents = True
End Sub
End Class
I used -1 to distinguish the new entry but you could use whatever you want here.
I'm sure there are many ways of doing this but hopefully it helps you out!Copyright (c) Marimer LLC