0
votes

I am working on vb.net windows application.
I am populating my DataGridView like this(this code i written in my load event)

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)

and datagridview cell content click i wrote code like this:

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"

i am trying to add image in my image column in the first row,but image coming in all row of image column.
i am getting image in data grid view like this enter image description here

I want to get image in particular row of particular column only..any help is very appreciable

1

1 Answers

1
votes

After setting the DataSource, create a DataGridViewImageColumn and add it to the DataGridView.

In the CellContentClick event handler write the below code and replace ** by the ImageColumn number and $$ by ButtonColumn number.

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