我有一个带有按钮和 datagridview 的 Windows 窗体。该项目包括一个有效的数据库连接和 LINQ to SQL 类。我正在尝试将 datagridview 绑定到 LINQ to SQL。
在一个代码模块中,我有这个:
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
而这个 button_click 代码的形式是:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.DataSource = DataGridList()
End Sub
当我单击按钮时,我会在 datagridview 中获得学校名称的长度,列标题为“长度”。
如果我只是在 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
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
For Each block In TestQuery
Debug.Print(block)
Next
End Sub