|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Binding to an enumerationI'd like to use an enumeration as a datasource for a drop-down box. Is there
a way to do this? Nathan,
Have you tried something like: Public Enum SomeEnum Value1 = 1 Value2 = 2 End Enum Dim list As Array = [Enum].GetValues(GetType(SomeEnum)) Then bind the above list variable? Some people bind to the names, via [Enum].GetNames, as opposed to the values. I prefer using the values as above In Windows Forms as the ComboBox.SelectedValue is the enum value rather then a string... In Web Forms I don't see an advantage to using the values, but then again I have not bound to enums in web forms... -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Nathan" <Nat***@discussions.microsoft.com> wrote in message news:CD812C76-D8B2-49AE-8194-6D5AD2095068@microsoft.com... | I'd like to use an enumeration as a datasource for a drop-down box. Is there | a way to do this? Nathan,
What I've pasted below is from the VS2005 Help documentation. It doesn't specify for which version of .NET it's for, so I can't guarantee it'll work for all versions. Dave _________________________________________________________ The following code example demonstrates how to bind a collection of objects to a DataGridView control so that each object displays as a separate row. This example also illustrates how to display a property with an enumeration type in a DataGridViewComboBoxColumn so that the combo box drop-down list contains the enumeration values. This example requires: a.. References to the System and System.Windows.Forms assemblies. Imports System.Windows.Forms Imports System.Collections.Generic Public Enum Title King Sir End Enum Public Class EnumsAndComboBox Inherits Form Private flow As New FlowLayoutPanel() Private WithEvents checkForChange As Button = New Button() Private knights As List(Of Knight) Private dataGridView1 As New DataGridView() Public Sub New() MyBase.New() SetupForm() SetupGrid() End Sub Private Sub SetupForm() AutoSize = True End Sub Private Sub SetupGrid() knights = New List(Of Knight) knights.Add(New Knight(Title.King, "Uther", True)) knights.Add(New Knight(Title.King, "Arthur", True)) knights.Add(New Knight(Title.Sir, "Mordred", False)) knights.Add(New Knight(Title.Sir, "Gawain", True)) knights.Add(New Knight(Title.Sir, "Galahad", True)) ' Initialize the DataGridView. dataGridView1.AutoGenerateColumns = False dataGridView1.AutoSize = True dataGridView1.DataSource = knights dataGridView1.Columns.Add(CreateComboBoxWithEnums()) ' Initialize and add a text box column. Dim column As DataGridViewColumn = _ New DataGridViewTextBoxColumn() column.DataPropertyName = "Name" column.Name = "Knight" dataGridView1.Columns.Add(column) ' Initialize and add a check box column. column = New DataGridViewCheckBoxColumn() column.DataPropertyName = "GoodGuy" column.Name = "Good" dataGridView1.Columns.Add(column) ' Initialize the form. Controls.Add(dataGridView1) Me.AutoSize = True Me.Text = "DataGridView object binding demo" End Sub Private Function CreateComboBoxWithEnums() As DataGridViewComboBoxColumn Dim combo As New DataGridViewComboBoxColumn() combo.DataSource = [Enum].GetValues(GetType(Title)) combo.DataPropertyName = "Title" combo.Name = "Title" Return combo End Function #Region "business object" Private Class Knight Private hisName As String Private good As Boolean Private hisTitle As Title Public Sub New(ByVal title As Title, ByVal name As String, _ ByVal good As Boolean) hisTitle = title hisName = name Me.good = good End Sub Public Property Name() As String Get Return hisName End Get Set(ByVal Value As String) hisName = Value End Set End Property Public Property GoodGuy() As Boolean Get Return good End Get Set(ByVal Value As Boolean) good = Value End Set End Property Public Property Title() As Title Get Return hisTitle End Get Set(ByVal Value As Title) hisTitle = Value End Set End Property End Class #End Region Public Shared Sub Main() Application.Run(New EnumsAndComboBox()) End Sub End Class Show quoteHide quote "Nathan" <Nat***@discussions.microsoft.com> wrote in message news:CD812C76-D8B2-49AE-8194-6D5AD2095068@microsoft.com... > I'd like to use an enumeration as a datasource for a drop-down box. Is > there > a way to do this? "Nathan" <Nat***@discussions.microsoft.com> wrote in message What we have done is created a method that takes a drop-down listbox news:CD812C76-D8B2-49AE-8194-6D5AD2095068@microsoft.com... > I'd like to use an enumeration as a datasource for a drop-down box. Is > there > a way to do this? control, enumeration type, and default value. For each enum value, we added a Description attribute that describes the value. Then instead of displaying the enumeration names/values, we display (using reflection) the values of the DescriptionAttribute for each enumeration value. This way, each item displayed in the drop down is displayed in a more descriptive way... HTH, Mythran "Nathan" <Nat***@discussions.microsoft.com> schrieb: See:> I'd like to use an enumeration as a datasource for a drop-down box. Is > there > a way to do this? <URL:http://groups.google.de/group/microsoft.public.dotnet.languages.vb/msg/6ded56f06760df69> -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> The following code binds an enum to a combo-box and then retrieves the Enum
that is selected in the combo-box: dim mySelectedEnum as myEnum 'Display the Enum names in the combo box myComboBox.DataSource = [Enum].GetNames(GetType(myEnum)) 'Get the selected Enum mySelectedEnum = CType([Enum].Parse(GetType(myEnum), _ myComboBox.SelectedItem.ToString), myEnum) Hope this helps. -- Show quoteHide quoteDennis in Houston "Nathan" wrote: > I'd like to use an enumeration as a datasource for a drop-down box. Is there > a way to do this? Dennis,
The following code "simplifies" this concept: | dim mySelectedEnum as myEnum Notice the actual bind is the same amount of code, however the get selected | | 'Display the Enum names in the combo box | myComboBox.DataSource = [Enum].GetValues(GetType(myEnum)) | | 'Get the selected Enum | mySelectedEnum = DirectCast(myComboBox.SelectedItem, myEnum) value is significantly simplified. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Dennis" <Den***@discussions.microsoft.com> wrote in message news:547F594D-2C20-477F-A3AB-486F84AE2003@microsoft.com... | The following code binds an enum to a combo-box and then retrieves the Enum | that is selected in the combo-box: | | dim mySelectedEnum as myEnum | | 'Display the Enum names in the combo box | myComboBox.DataSource = [Enum].GetNames(GetType(myEnum)) | | 'Get the selected Enum | mySelectedEnum = CType([Enum].Parse(GetType(myEnum), _ | myComboBox.SelectedItem.ToString), myEnum) | | Hope this helps. | -- | Dennis in Houston | | | "Nathan" wrote: | | > I'd like to use an enumeration as a datasource for a drop-down box. Is there | > a way to do this? Thanks for shortened tip. I was really trying to write it such that it was
general for comboboxes that were only strings that would convert a string entered into a Enum. -- Show quoteHide quoteDennis in Houston "Jay B. Harlow [MVP - Outlook]" wrote: > Dennis, > The following code "simplifies" this concept: > > | dim mySelectedEnum as myEnum > | > | 'Display the Enum names in the combo box > | myComboBox.DataSource = [Enum].GetValues(GetType(myEnum)) > | > | 'Get the selected Enum > | mySelectedEnum = DirectCast(myComboBox.SelectedItem, myEnum) > > Notice the actual bind is the same amount of code, however the get selected > value is significantly simplified. > > -- > Hope this helps > Jay B. Harlow [MVP - Outlook] > ..NET Application Architect, Enthusiast, & Evangelist > T.S. Bradley - http://www.tsbradley.net > > > "Dennis" <Den***@discussions.microsoft.com> wrote in message > news:547F594D-2C20-477F-A3AB-486F84AE2003@microsoft.com... > | The following code binds an enum to a combo-box and then retrieves the > Enum > | that is selected in the combo-box: > | > | dim mySelectedEnum as myEnum > | > | 'Display the Enum names in the combo box > | myComboBox.DataSource = [Enum].GetNames(GetType(myEnum)) > | > | 'Get the selected Enum > | mySelectedEnum = CType([Enum].Parse(GetType(myEnum), _ > | myComboBox.SelectedItem.ToString), myEnum) > | > | Hope this helps. > | -- > | Dennis in Houston > | > | > | "Nathan" wrote: > | > | > I'd like to use an enumeration as a datasource for a drop-down box. Is > there > | > a way to do this? > > >
hierachical related data in datatset
VB2005 Pro Edition--Data Sources window is disabled when viewing forms Adding a Web Reference changes DataTypes ??? Multithreading Problem Traversing a Collection evaluate or transform or ??? Regular Expression Accessing one dimension of a multidimensional array Desperately needing direction with EncoderParameters and saving images under different compression/q Maximum Form Height? |
|||||||||||||||||||||||