本文介绍了linq查询到datagridview,为什么我看到包含数字的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我运行此代码时,为什么不用"Months"填充"month"列,为什么我看到一个用数​​字填充的名为length的列,而我的qry从来没有要求过.

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


这是根据使用类"的建议而更新的解决方案,希望通过减少看起来有点长的代码行来建议.



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.

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

推荐答案

Public Class MyValue(Of T)
   Public Property Value As T
End Class

现在,您可以将String放在其中并绑定到.
希望对您有所帮助:)

Now you can put your String in there and bind to the MyValue Class.
Hope it helps :)



这篇关于linq查询到datagridview,为什么我看到包含数字的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:27