实际上我想根据我的要求自定义 Datagridview 单元格。我必须将文本和图像添加到特定列的 Datagridview 行。见下图
谁能帮我举个例子?谢谢。
实际上我想根据我的要求自定义 Datagridview 单元格。我必须将文本和图像添加到特定列的 Datagridview 行。见下图
谁能帮我举个例子?谢谢。
尝试这个
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataGridView1.ColumnCount = 3
DataGridView1.Columns(0).Name = "Product ID"
DataGridView1.Columns(1).Name = "Product Name"
DataGridView1.Columns(2).Name = "Product_Price"
Dim row As String() = New String() {"1", "Product 1", "1000"}
DataGridView1.Rows.Add(row)
row = New String() {"2", "Product 2", "2000"}
DataGridView1.Rows.Add(row)
row = New String() {"3", "Product 3", "3000"}
DataGridView1.Rows.Add(row)
row = New String() {"4", "Product 4", "4000"}
DataGridView1.Rows.Add(row)
Dim img As New DataGridViewImageColumn()
Dim inImg As Image = Image.FromFile("Image Path")
img.Image = inImg
DataGridView1.Columns.Add(img)
img.HeaderText = "Image"
img.Name = "img"
End Sub
End Class
Dim dt As New DataTable
Dim imageColumn As New DataColumn
imageColumn.ColumnName = "ActualImage"
imageColumn.DataType = GetType(System.Drawing.Image)
dt.Columns.Add(imageColumn)
For Each row As DataRow In dt.Rows
row("ActualImage") = System.Drawing.Image.FromFile(row("Image_Path"))
Next
dt.AcceptChanges()
Dim dgvImageColumn As New DataGridViewImageColumn
dgvImageColumn.DataPropertyName = "ActualImage"
dgvImageColumn.Name = "ActualImage"
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
DataGridView1.Columns.Add(dgvImageColumn)
DataGridView1.DataSource = dt
您可以动态地将 DataGridView 单元格转换DataGridViewTextBoxCell()
为该列并将文本值显示到该列。下面是代码示例,可以为您提供一些基本概念。
Private Sub button1_Click(sender As Object, e As EventArgs)
dataGridView1.Rows(3).Cells("ImageCol") = New DataGridViewTextBoxCell()
dataGridView1.Rows(3).Cells("ImageCol").Value = "Hello.."
End Sub