Solved

VB.net Binding DataGridViewComboBoxCell to Enumeration

Posted on 2010-08-21
3
2,107 Views
Last Modified: 2012-05-10
I have the following code to define and set up my enumeration which I want to bind to a datagridviewcomboboxcell

<System.SerializableAttribute(), _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="")> _
Partial Public Class WorkingDaysOfWeek
    Private iDay As iWeekDay
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
    Public Property HELDINACCT() As iWeekDay
        Get
            Return Me.iDay
        End Get
        Set(ByVal value As iWeekDay)
            Me.iDay = value
        End Set
    End Property

    <System.SerializableAttribute(), _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="")> _
    Enum iWeekDay
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
    End Enum

End Class

Open in new window


I then use this routine to bind the enumeration to the datagridviewcomboboxcolumn

        Dim iSa As New DataGridViewComboBoxColumn
        With iSA
            .DataSource = System.Enum.GetValues(GetType(WorkingDaysOfWeek.iWeekDay))
            .Name = "Days"
            .FlatStyle = FlatStyle.Flat
        End With

Open in new window


My question is:

I am using VB 2010 Express edition and whenever I select a value from any cell in the combo column, the DISPLAYED value always returns to Monday (notwithstanding that I may have chosen another day).

Is this because of the way I have defined my class and enumeration, or is there some setting I have to make in the datagridview itself?

PS. I use the class above as part of a bigger class that serializes to XML. Incidentally, all enumerations in that class (sisters of the above) behave in the same way when bound to a DataGridviewComboboxCell / Column.
0
Comment
Question by:nepaluz
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33494262
try this
Public Shared Function BindEnumeration(ByVal _enum As Type) As ArrayList
        'get the names from the enumeration
        Dim enumNames() As String = System.Enum.GetNames(_enum)
        'get the values from the enumeration
        Dim enumValues() As Integer = System.Enum.GetValues(_enum)
        'turn it into a hash table
        Dim aList As New ArrayList()
        For i As Integer = 0 To enumNames.Length - 1
            aList.Insert(enumValues(i), enumNames(i))
        Next
        Return aList
    End Function
    Enum iWeekDay
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
    End Enum
DatsGridViewComboBox.DataSource = BindEnumeration(GetType(iWeekDay))

Open in new window

0
 
LVL 17

Author Comment

by:nepaluz
ID: 33494339
jtoutou - thanks for the response.

Your code (should) work, however, it does not take into consideration of the "duo" purpose of my code request and question.

1. In the first place, the class serialize to XML (your suggested solution does not cater for that).

2. The second purpose is to have a working comboboxcell. My code above DOES produce a combocell bound to the enumeration (again as illustrated), however, there seems to be an issue with the displyed value (after selecting from the combo), which was my request for help (NOT INDEPENDENT OF THE SERIALIZATION!).
I may be missing something from your code, but may I request a more complete code sample with serialization included?

0
 
LVL 17

Author Closing Comment

by:nepaluz
ID: 33540586
It should have been obvious (having tested the code) that this was the solution, and it indeed is the solution.

jtoutou - could you be a bit more verbose next time PSE!???
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 42
VB.net Facial recognition software (time and attendance) 6 38
No Data for DropDown List 2 24
Create a datatable in vb.net dynamically 1 14
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question