vb.net 如何在datagridview单元格中添加图像和文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14476558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Add the image and text in datagridview cell
提问by Rahul Shirphule
Actually i want to customised the Datagridview cell according to my requirement. I've to add text and image to Datagridview row for specific column. see the following image
实际上我想根据我的要求自定义 Datagridview 单元格。我必须为特定列的 Datagridview 行添加文本和图像。见下图


Can any one help me with some example ? Thanks.
任何人都可以帮我举一些例子吗?谢谢。
回答by Rachel Gallen
try this
尝试这个
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
回答by Niraj Trivedi
You can you convert DataGridView cell dynamically to DataGridViewTextBoxCell()and display text value to that column. below is code sample that gives you some basic idea.
您可以将 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
回答by nanda9894
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

