Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

Dim Months = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

        Dim DGView As New DataGridView
        Me.Controls.Add(DGView) 'Add datagridview
        DGView.ColumnCount = 1 ' add a column
        DGView.Columns(0).Name = "Month" 'add a column name
        DGView.Dock = DockStyle.Fill 'set dock style

        'linq query
        Dim Q = From M In Months
                   Select M

        DGView.DataSource = Months


    End Sub


when I run this code why isnt month column populated with "Months" and why do I see a column called length which is populated with numbers, which I never asked for with my qry.

-----------------------------------------------------------------------------------


Here is updated solution following suggestion to use a "Class", would like suggestions to reduce lines of code as it looks a bit long.

VB
Public Class Form1
    Public Class Year
        Public Property Months As String
    End Class

    Public Sub OrderByMonth()
        Dim Month1 As New Year With {.Months = "January"}
        Dim Month2 As New Year With {.Months = "Febuary"}
        Dim Month3 As New Year With {.Months = "March"}
        Dim Month4 As New Year With {.Months = "April"}
        Dim Month5 As New Year With {.Months = "May"}
        Dim Month6 As New Year With {.Months = "June"}
        Dim Month7 As New Year With {.Months = "July"}
        Dim Month8 As New Year With {.Months = "August"}
        Dim Month9 As New Year With {.Months = "September"}
        Dim Month10 As New Year With {.Months = "October"}
        Dim Month11 As New Year With {.Months = "November"}
        Dim Month12 As New Year With {.Months = "December"}

        Dim ArrList As New ArrayList()
        ArrList.Add(Month1)
        ArrList.Add(Month2)
        ArrList.Add(Month3)
        ArrList.Add(Month4)
        ArrList.Add(Month5)
        ArrList.Add(Month6)
        ArrList.Add(Month7)
        ArrList.Add(Month8)
        ArrList.Add(Month9)
        ArrList.Add(Month10)
        ArrList.Add(Month11)
        ArrList.Add(Month12)

        ' Use an explicit type for non-generic collections 
        Dim Q = From R As Year In ArrList
                        Select R Order By R.Months

        DataGridView1.DataSource = Q.ToList

    End Sub

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        OrderByMonth()
    End Sub
End Class
Posted
Updated 10-Mar-12 9:22am
v3

You're actually binding to the String Object[^], which has a Property Length[^], but no Property which contains the String itself (thus the name of the month). It has the Default Property Chars[^], but this cannot be bound to.
I've had this problem when binding to Strings and Integers.
In your case you could create a DataTable[^] first, fill it with the names of the months (using a For Each Loop[^]) and then bind to that.
An alternative is to create a Class which stores your String as a value and bind to that, like so:
VB
Public Class MyValue(Of T)
   Public Property Value As T
End Class
Now you can put your String in there and bind to the MyValue Class.
Hope it helps :)
 
Share this answer
 
v2
Comments
Simon_Whale 7-Mar-12 4:50am    
Nice answer +5
Sander Rossel 7-Mar-12 13:11pm    
Thanks :)
It is what it is 10-Mar-12 15:19pm    
Ok, so I took your advise and went with the "Class" option, I stole bits of code from here and there and cobbled together the solution and added it to the original question.

However; not sure if you would have done it the same??, and it seems a bit long and wordy could you suggest a way to reduce code?
Sander Rossel 10-Mar-12 19:07pm    
How about:

Public Class Year
Public Sub New(Byval month As String)
Me.Month = month
End Sub
Public Property Month As String
End Class

Now in your Form you can do:
Dim list As New List(Of Year)
list.Add(New Year("January"))
list.Add(New Year("February"))
list.Add(New Year("March"))

...Etc.
This approach saves you half of the lines of code :)
I really suggest you use a List(Of T) instead of ArrayList too.
It is what it is 10-Mar-12 20:55pm    
Sweet :-)
Something similar was asked Here[^]

And Luc Pattyn states here in his reply[^]

Databinding to the datagridview uses reflection to build the results, hence this sort of results. So Naerlings answer would solve your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900