英文:
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_Load(sender As Object,e As EventArgs)处理 MyBase.Load
AddColumnsProgrammatically()
_myTable.Columns.Add("", GetType(Image()))
_myTable.Columns.Add("No", GetType(Integer))
_myTable.Columns.Add("Codeproduct", GetType(String))
LoadData()
结束子
私有子TextBox1_KeyPress(sender As Object,e 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(阿凡达,iRow,TextBox1.Text)
Grid.DataSource = _myTable
TextBox1.Clear()
结束如果
结束子
结束类
结束类
公共类项目
公共属性编号()作为整数
公共属性CodeProduct()作为字符串
公共函数GetAvatar()作为DataGridViewAvatar
返回新的DataGridViewAvatar() With {
.Value = CType(Me.CodeProduct, String),
.Icon = Image.FromFile("money.png")
}
结束函数
结束类
英文:
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
答案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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论