为什么 form2 中的 datagridview 在 VB.NET 中没有出现带有 dapper MS-Acess 的人口

why datagridview in form2 doesn't appear population with dapper MS-Acess in VB.NET

提问人:roy 提问时间:7/12/2023 最后编辑:roy 更新时间:7/13/2023 访问量:78

问:

为什么 Form2 中的 DataGridView 在 VB.NET 中没有出现带有 dapper MS-Acess 的人口?

如果我将数据源 datagridview 更改为函数,则填充将显示在 datagridview 中。我的代码有问题吗?请引导GetItem()

谢谢

'iN Form1

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.Show()
    End Sub

'in FORM2
 Private iService As New Itemservice2()
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = iService.Getitem2(Form1.Btxtcodeproduct.Text)
    End Sub
End Class
 Public Function Getitem() As IEnumerable(Of Stocks)
        Dim sql = "SELECT * FROM Stocks"
        Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
            Return _conn.Query(Of Stocks)(sql).ToList()
        End Using
    End Function
 Public Function Getitem2(ByVal code As String) As Stocks
        Dim sql = $"SELECT * FROM Stocks WHERE CodeProduct = '{code}'"
        Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
            Return _conn.Query(Of Stocks)(sql).FirstOrDefault()
        End Using
    End Function
Public Class Stocks
    Public Property Id() As Integer
    Public Property CodeProduct() As String
    Public Property Colorcode() As String
    Public Property Size() As String
    Public Property Qty() As Integer

End Class

result in datagridview in form2

使用函数 getItem() 在 Form2 中生成 DataGridView

result datagridview in form2 with function getitem()

vb.net ms-access datagridview dapper

评论

0赞 Olivier Jacot-Descombes 7/12/2023
在 Access 中手动运行 SQL,并查看它返回的内容。然后设置一个断点,并查看它分配给 DataSource 的内容。我们无法为您调试
0赞 roy 7/12/2023
@OlivierJacot-Descombes ,如果我使用函数运行,则填充会出现在 datagridview 中,但如果我使用函数运行,则 datagridview 中不会出现填充DataGridView1.DataSource = iService.Getitem()GetItem()GetItem2()
1赞 Olivier Jacot-Descombes 7/12/2023
你希望我做什么?我无法测试您的查询。
3赞 dr.null 7/12/2023
你好!不能将网格绑定到 的实例,而是绑定到 ,即使它包含单个项目也是如此。换言之,必须返回 .就像你在.StocksList<Stocks>Getitem2List<Stocks>Getitem
1赞 roy 7/13/2023
@dr.null,按照你的建议,所以你的导游走得很完美You can't bind the grid to an instance of Stocks, but to a List<Stocks> even if it contains a single item. In other words, Getitem2 must return a List<Stocks>Public Function Getitem2(ByVal code As String) As IEnumerable(Of Stocks)Return _conn.Query(Of Stocks)(sql).ToList()

答:

1赞 roy 7/13/2023 #1

根据 @dr.null 的准则 然后我得到了完美的答案

 Public Function Getitem2(ByVal code As String) As IEnumerable(Of Stocks)
        Dim sql = $"SELECT * FROM Stocks WHERE CodeProduct = '{code}'"
        Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
            Return _conn.Query(Of Stocks)(sql).ToList()
        End Using
    End Function