LINQ to SQL datagridview查询返回长度,而不是值 [英] LINQ to SQL datagridview query returns length, not value
问题描述
在一个代码模块中,我已经得到了:
公共函数DataGridList()作为BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
从块中db.BLOCK_ASSIGNMENTs
其中Block.gr912_school =Franklin
选择Block.gr6_school不同的
返回NewBindingSource
结束函数
这个button_click代码的形式如下:
code> Private Sub Button1_Click(sender As System.Object,e As System.EventArgs)Handles Button1.Click
DataGridView1.DataSource = DataGridList()
End Sub
当我点击按钮时,我得到datagridview中学校名称的长度,列标题为length。
如果我只是在button_click中运行这个非常相似的代码,学校名称会在立即窗口中正确显示:
Private Sub Button1_Click(sender As System.Object,e As System.EventArgs)Handles Button1.Click
Dim db As New DataClasses1DataContext()
Dim TestQuery =
From Block In db .BLOCK_ASSIGNMENTs
其中Block.gr912_school =Franklin
选择Block.gr6_school不同的
对于每个块在TestQuery
Debug.Print(块)
下一个
End Sub
:
公共函数DataGridList()作为BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
从块在db.BLOCK_ASSIG NMENTs
其中Block.gr912_school =Franklin
使用{Key .Value = Block.gr6_school}选择新增不同的
返回NewBindingSource
结束函数
这应该给它一个DataGridView可以接收的属性。 New With
...创建一个名为Value的Property的匿名对象。 DataGridView通过枚举公共属性并将其呈现为列,对此类型的对象进行工作。如果您需要多个值,则可以以大括号内的其他项目以逗号分隔。有关详细信息,请参阅匿名类型(Visual Basic)。
I've got a Windows Form with a button and a datagridview. The project includes a working database connection and LINQ to SQL class. I'm trying to bind the datagridview to the LINQ to SQL.
In a code module I've got this:
Public Function DataGridList() As BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
Return NewBindingSource
End Function
And this button_click code in the form:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.DataSource = DataGridList()
End Sub
When I click the button I get the length of the school names in the datagridview, with a column header of "length."
If I just run this very similar code in the button_click instead, the school names appear correctly in the immediate window:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim db As New DataClasses1DataContext()
Dim TestQuery =
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
For Each block In TestQuery
Debug.Print(block)
Next
End Sub
Give this a try:
Public Function DataGridList() As BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select New With { Key .Value = Block.gr6_school } Distinct
Return NewBindingSource
End Function
This should give it a property that the DataGridView can pick up on. The New With
... creates an anonymous object with a Property named Value. The DataGridView works on this type of object by enumerating the public properties and rendering them as columns. If you had needed more than one value, you could have added additional items inside the curly braces in the same way separated by commas. See Anonymous Types (Visual Basic) for more information.
这篇关于LINQ to SQL datagridview查询返回长度,而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!