Home > Blockchain >  How to display image in pictureedit and picturebox when clicked in gridview devexpress in vb.net
How to display image in pictureedit and picturebox when clicked in gridview devexpress in vb.net

Time:02-22

I want to display images in picturedit and picturebox when I click on gridview devexpress. I want to display the image when the cell value clicked in the path1 column and path2 column in one pictureedit and one picturebox.

Public Class Form1
    Private WithEvents dt As New DataTable
    Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\PRODUCT.mdb"
    Dim cn = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path
    Private Images As Hashtable = New Hashtable()

    Private Sub LoadDataGridView()
        Try
            dt = New DataTable
            Dim query = "select Code,Path1,Path2 FROM ITEM"

            Using adapter As New OleDbDataAdapter(query, cn.ToString)
                adapter.Fill(dt)
            End Using
            Me.GridControl1.DataSource = dt
        Catch myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadDataGridView()
    End Sub

     Private Sub GridView1_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles GridView1.RowCellClick
        Dim view As GridView = TryCast(sender, GridView)
        Dim path1 As String = view.GetFocusedRowCellValue("Path1").ToString
        Dim path2 As String = view.GetFocusedRowCellValue("Path2").ToString
        PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(path1)), True, False)
        PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(path2)), True, False)
    End Sub
End Class

Capturegridview18022022

Captureerror18022022

CodePudding user response:

Use Click event instead of RowCellClick

Private Sub GridView1_Click(sender As Object, e As RowCellClickEventArgs) Handles GridView1.Click
    Dim path1 As String = (GridView1.GetRowCellValue(GridView1.FocusedRowHandle,"Path1"))
    Dim path2 As String = (GridView1.GetRowCellValue(GridView1.FocusedRowHandle,"Path2"))
    PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(path1)), True, False)
    PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(path2)), True, False)
End Sub

CodePudding user response:

Private Sub GridView1_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles GridView1.RowCellClick
If e.Column.FieldName = "Path1" Then
            Dim view As GridView = TryCast(sender, GridView)
            Dim path1 As String = view.GetRowCellValue(e.RowHandle, e.Column).ToString
            PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(path1)), True, False)
        Else
            If e.Column.FieldName = "Path2" Then
                Dim view As GridView = TryCast(sender, GridView)
                Dim path2 As String = view.GetRowCellValue(e.RowHandle, e.Column).ToString
                PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(path2)), True, False)
            End If

        End If
    End Sub
  • Related