将图像添加到数据网格视图在Windows窗体应用程序中不正确

时间:2014-01-20 05:32:17

标签: c# vb.net winforms datagridview

我正在使用vb.net windows应用程序 我正在填充DataGridView这样的代码(这个代码我在我的加载事件中写的)

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 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 btn As New DataGridViewButtonColumn
        btn.HeaderText = "Image"
        btn.Text = "...."
        btn.Name = "btn"
        btn.UseColumnTextForButtonValue = True
        gv.Columns.Insert(6, btn)

和datagridview单元格内容点击我编写的代码如下:

Dim OFDLogo As New OpenFileDialog()
            OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
            If OFDLogo.ShowDialog() = DialogResult.OK Then
                txtCorLogo.Text = OFDLogo.FileName()
            End If 
 Dim path As String = txtCorLogo.Text
 Dim img As New DataGridViewImageColumn()
        Dim inImg As Image = Image.FromFile(path)
        img.Image = inImg
        gv.Columns.Add(img)
        img.HeaderText = "Image"
        img.Name = "img"

我正在尝试在第一行的图像列中添加图像,但图像列在图像列的所有行中  我正在这样的数据网格视图中获取图像 enter image description here

我想在特定列的特定行中获取图像。任何帮助都非常明显

1 个答案:

答案 0 :(得分:1)

设置DataSource后,创建DataGridViewImageColumn并将其添加到DataGridView

在CellContentClick事件处理程序中编写以下代码,并用ImageColumn编号替换**,用ButtonColumn编号替换$$。

If e.CoumnIndex == $$ Then
Dim OFDLogo As New OpenFileDialog()
        OFDLogo.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp"
        If OFDLogo.ShowDialog() = DialogResult.OK Then
          myDataGridView.Rows(e.RowIndex).Cells(**).Value = Image.FromFile(aOFD.FileName);
        End If 
End If