如何在vb.net中将DataTable图像列添加或填充到DataGridView图像列

huangapple go评论67阅读模式
英文:

How to Add or Fill DataTable Image Column to DataGridView Image Column in vb.net

问题

我想询问一下我的代码是否有什么问题以及如何将数据正确地填充到DataGridView中的datatable中

谢谢

私有子装载数据()
        Dim item As New List(Of 项目())
        Grid.Bind(item)
    结束子

    私有子填充数据表阿凡达作为图像iRow作为整数ByVal Codeproduct作为字符串
        Dim row As DataRow = _myTable.NewRow()
        row("") = 阿凡达
        row("No") = iRow
        row("Codeproduct") = Codeproduct
        _myTable.Rows.Add(row)
    结束子

    私有子以编程方式添加列()
        Dim Col1 = New DataGridViewImageColumn()
        Dim Col2 = New DataGridViewTextBoxColumn()
        Dim Col3 = New DataGridViewTextBoxColumn()
        Col2.HeaderText = ""
        Col2.Name = "Column1"
        Col2.DataPropertyName = "{GetAvatar()}"
        Col2.HeaderText = "No"
        Col2.Name = "Column2"
        Col2.DataPropertyName = "No"
        Col3.HeaderText = "CodeProduct"
        Col3.Name = "Column3"
        Col3.DataPropertyName = "CodeProduct"
        Grid.Columns.AddRange(New DataGridViewColumn() {Col1, Col2, Col3})
    结束子

    私有子Form1_Loadsender As Objecte As EventArgs处理 MyBase.Load
        AddColumnsProgrammatically()
        _myTable.Columns.Add("", GetTypeImage()))
        _myTable.Columns.Add("No", GetTypeInteger)
        _myTable.Columns.Add("Codeproduct", GetTypeString)
        LoadData()
    结束子

    私有子TextBox1_KeyPresssender As Objecte As KeyPressEventArgs处理 TextBox1.KeyPress

        如果 e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) 然后
            整数iRow
            如果 Grid.RowCount - 1 = 0 然后
                iRow = 1
            其他
                iRow = Convert.ToInt32(Grid.Rows(Grid.RowCount - 2).Cells(1).Value.ToString()) + 1
            结束如果
            填充DataTable阿凡达iRowTextBox1.Text
            Grid.DataSource = _myTable
            TextBox1.Clear()
        结束如果
    结束子
结束类
结束类
公共类项目
    公共属性编号()作为整数
    公共属性CodeProduct()作为字符串
    公共函数GetAvatar()作为DataGridViewAvatar
        返回新的DataGridViewAvatar() With {
        .Value = CType(Me.CodeProduct, String),
        .Icon = Image.FromFile("money.png")
    }
    结束函数
结束类

如何在vb.net中将DataTable图像列添加或填充到DataGridView图像列

英文:

I want to ask if there is anything wrong with my code, and how can I fill datatable to DataGridView with this data properly.

Thanks

  Private Sub LoadData()
Dim item As New List(Of item())
Grid.Bind(item)
End Sub
Private Sub FillDataTable(Avatar As Image, iRow As Integer, ByVal Codeproduct As String)
Dim row As DataRow = _myTable.NewRow()
row("") = Avatar
row("No") = iRow
row("Codeproduct") = Codeproduct
_myTable.Rows.Add(row)
End Sub
Private Sub AddColumnsProgrammatically()
Dim Col1 = New DataGridViewImageColumn()
Dim Col2 = New DataGridViewTextBoxColumn()
Dim Col3 = New DataGridViewTextBoxColumn()
Col2.HeaderText = ""
Col2.Name = "Column1"
Col2.DataPropertyName = "{GetAvatar()}"
Col2.HeaderText = "No"
Col2.Name = "Column2"
Col2.DataPropertyName = "No"
Col3.HeaderText = "CodeProduct"
Col3.Name = "Column3"
Col3.DataPropertyName = "CodeProduct"
Grid.Columns.AddRange(New DataGridViewColumn() {Col1, Col2, Col3})
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddColumnsProgrammatically()
_myTable.Columns.Add("", GetType(Image()))
_myTable.Columns.Add("No", GetType(Integer))
_myTable.Columns.Add("Codeproduct", GetType(String))
LoadData()
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
Dim iRow As Integer
If Grid.RowCount - 1 = 0 Then
iRow = 1
Else
iRow = Convert.ToInt32(Grid.Rows(Grid.RowCount - 2).Cells(1).Value.ToString()) + 1
End If
FillDataTable(avatar, iRow, TextBox1.Text)
Grid.DataSource = _myTable
TextBox1.Clear()
End If
End Sub
End Class
End Class
Public Class item
Public Property No() As Integer
Public Property CodeProduct() As String
Public Function GetAvatar() As DataGridViewAvatar
Return New DataGridViewAvatar() With {
.Value = CType(Me.CodeProduct, String),
.Icon = Image.FromFile("money.png")
}
End Function
End Class

如何在vb.net中将DataTable图像列添加或填充到DataGridView图像列

答案1

得分: 1

这是 一个链接!从文档链接中我找到了解决方案

    itemtransfer.Add(New ItemTransfer With {
                            .No = iRow,
                            .CodeProduct = BtxtBarcode.Text
                            })
    grid.Bind(itemtransfer)
英文:

Here's a link!
from the documentation link I got the solution

    itemtransfer.Add(New ItemTransfer With {
.No = iRow,
.CodeProduct = BtxtBarcode.Text
})
grid.Bind(itemtransfer)

huangapple
  • 本文由 发表于 2023年6月19日 10:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定