Click here to Skip to main content
15,887,930 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone, can you help me with this please see the photo below. I want to add items with images on the datagrid from a database but only when i search the item and click add at first click on add its correct. but when i search an item again and click add it will duplicate the columns.

What I have tried:

Public Sub retrieveItems()
'Retrieve Image from database
' dgvQuotation.Columns.Clear()

Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader

Try
dgvQuotation.RowTemplate.Height = 100
Dim dgvImage = New DataGridViewImageColumn()
dgvImage.HeaderText = "Image"
dgvImage.ImageLayout = DataGridViewImageCellLayout.Stretch

Dim dgvCode As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
dgvCode.HeaderText = "Code"

Dim dgvName As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
dgvName.HeaderText = "Name"
Dim dgvDesc As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
dgvDesc.HeaderText = "Description"
Dim dgvRetail As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
dgvRetail.HeaderText = "Retail"
Dim dgvTrade As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn()
dgvTrade.HeaderText = "Trade"
dgvQuotation.Columns.Add(dgvCode)
dgvQuotation.Columns.Add(dgvName)
dgvQuotation.Columns.Add(dgvDesc)
dgvQuotation.Columns.Add(dgvRetail)
dgvQuotation.Columns.Add(dgvTrade)
dgvQuotation.Columns.Add(dgvImage)

dgvQuotation.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgvQuotation.RowTemplate.Height = 120
dgvQuotation.AllowUserToAddRows = False

conDB.Close()
connectDB()
cmd.Connection = conDB
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from items where item_code='" & frmManageItem.dgvitems.SelectedCells(1).Value & "'"
dr = cmd.ExecuteReader

If Not dr Is Nothing Then
dr.Read()
Dim qcode As String
Dim qname As String
Dim qdesc As String
Dim qretail As String
Dim qtrade As String
qcode = dr("item_code").ToString
qname = dr("item_name").ToString
qdesc = dr("item_description").ToString
qretail = dr("retail_price").ToString
qtrade = dr("trade_price").ToString

Dim data As Byte() = DirectCast(dr("Photo"), Byte())
Dim ms As New MemoryStream(Data)
PictureBox2.Image = Image.FromStream(ms)
'PictureBox2.Image.Save(ms, PictureBox2.Image.RawFormat)

Dim md As MemoryStream = New MemoryStream()
PictureBox2.Image.Save(md, PictureBox2.Image.RawFormat)

Dim img As Byte() = ms.ToArray()

dgvQuotation.Rows.Add(qcode, qname, qdesc, qretail, qtrade, img)
dr.Close()

For i As Integer = 0 To dgvQuotation.Columns.Count - 1
If TypeOf dgvQuotation.Columns(i) Is DataGridViewImageColumn Then
DirectCast(dgvQuotation.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
End If

Next
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conDB.Close()
End Try
End Sub
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900