0

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 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

sir in the load event it self i have to add DataGridViewImageColumn
sir,,this is working fine..but i have one more doubt...can i ask?
i want to activate cell content click of my image column,or i want to de-activate cell content click of first 4 column
I have update the code, check it.. don't forget to vote up ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.