如何在datagridview单元格中添加图像和文本

时间:2013-01-23 09:42:52

标签: vb.net image datagridview

实际上我想根据我的要求自定义Datagridview单元格。 我要为特定列添加文本和图像到Datagridview行。 见下图

enter image description here

任何人都能帮我一些例子吗? 感谢。

3 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

您可以将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

答案 2 :(得分:0)

       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