vb.net在datagridview中插入带有image的行

时间:2014-07-26 14:40:40

标签: vb.net

如果列是图像列,如何向数据网格视图插入行?

如果所有内容都是文本列,那么它只是

 DataGridView1.Rows.Insert(0, "A", "B", "C")
 DataGridView1.Rows.Insert(1, "D", "E", "F")

但是,如果第一列是图像列并且我不想在其上显示任何图像,该怎么办

2 个答案:

答案 0 :(得分:0)

http://msdn.microsoft.com/en-US/library/system.windows.forms.datagridviewimagecolumn.aspx

  

默认情况下,空单元格显示默认错误图形。若要防止此图形显示为等于null或DBNull.Value的单元格值,请在向控件添加行之前将DefaultCellStyle属性返回的单元格样式对象的DataGridViewCellStyle.NullValue属性设置为null。但是,这不会影响新记录的行。为了防止在控件AllowUserToAddRows属性值为true时错误图形出现在新记录的行中,您还必须在控件RowsAdded事件的处理程序中将单元格值显式设置为null,或者将列CellTemplate属性设置为实例DataGridViewImageCell派生类型的一个重写的DefaultNewRowValue属性返回null。

因此,首先设置DataGridViewCellStyle.NullValue Nothing

DataGridView1.Columns(0).DefaultCellStyle.NullValue = Nothing

然后传递 Nothing 作为Rows.Insert的图像列的参数:

DataGridView1.Rows.Insert(0, Nothing, "B", "C")

如果AllowUserToAddRows DataGridView1 True ,请按照上述步骤操作,以防止错误图形显示在新记录的行中。

答案 1 :(得分:0)

将图像列插入已设计的带有默认图像的datagridView的代码:

        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"

参考:http://vb.net-informations.com/datagridview/vb.net_datagridview_image.htm