提问人:roy 提问时间:7/12/2023 最后编辑:roy 更新时间:7/13/2023 访问量:78
为什么 form2 中的 datagridview 在 VB.NET 中没有出现带有 dapper MS-Acess 的人口
why datagridview in form2 doesn't appear population with dapper MS-Acess in VB.NET
问:
为什么 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
使用函数 getItem() 在 Form2 中生成 DataGridView
答:
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
评论
DataGridView1.DataSource = iService.Getitem()
GetItem()
GetItem2()
Stocks
List<Stocks>
Getitem2
List<Stocks>
Getitem
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()