系统格式异常“数据网格视图中从System.string到system.drawing.image的无效转换

时间:2014-01-21 07:58:51

标签: c# vb.net winforms datagridview

我有像这样的数据网格视图。我使用数据源绑定enter image description here填充数据网格视图

虽然绑定我得到一个额外的列emp图像..实际上我想在 image 列本身显示我的图像..所以我在加载事件中给出了这样的代码

Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
adapter = New SqlDataAdapter("select c.cid,c.CompanyName,d.dtId,d.dtName as Department,d.dtPhone as Phone,d.dtEmail as Email,d.empimage from CompanyMaster_tbl c join  DepartmentMaster_tbl d on c.Cid=d.cId order by cid", con.connect)
dt1 = New DataTable
bSource = New BindingSource
adapter.Fill(dt1) 'Filling dt with the information from the DB
bSource.DataSource = dt1
gv.DataSource = bSource
gv.Columns("cid").Visible = False
gv.Columns("dtId").Visible = False
Dim img As New DataGridViewImageColumn
img.HeaderText = "image"
gv.Columns.Insert(6, img)
For i As Integer = 0 To gv.Rows.Count - 1
    gv.Rows(i).Cells(6).Value = gv.Rows(i).Cells(7).Value
Next
gv.Columns("empimage").Visible = False

actullay在这里我将图像从empimage列复制到image列,然后使empimage可见false ..保存后我再次调用此方法..那时候得到的错误就像这样:enter image description here

1 个答案:

答案 0 :(得分:0)

而不是执行以下三个步骤

  1. 添加image
  2. empimage列复制到image
  3. 隐藏empimage
  4. 不要添加图像列并更改查询以为empimage列指定别名,如下所示

      

    选择c.cid,c.CompanyName,d.dtId,d.dtName作为Department,d.dtPhone作为Phone,d.dtEmail作为Email,d.empimage作为图像来自CompanyMaster_tbl c加入DepartmentMaster_tbl d on c.Cid = d.cId by cid