linq查询到datagridview,为什么我看到包含数字的列? [英] linq query to datagridview, why do I see a column which contains numbers?

查看:76
本文介绍了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

推荐答案

您实际上是绑定到字符串对象 [ ^ ],其具有属性长度 [默认属性字符 [ DataTable [对于每个循环 [
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:
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 :)


有人问了类似的问题回复中指出[ ^ ]

与datagridview的数据绑定使用反射来构建结果,因此是这种结果.因此,Naerlings的答案将解决您的问题.
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆