Flexgrid cell show picture from database

Posted by: saidnai on 15 January 2018, 11:47 pm EST

    • Post Options:
    • Link

    Posted 15 January 2018, 11:47 pm EST

    Dear all,

    It is a long time i have not been in the Forum. It seems that the layout has been changed. I cannot recognise the structure any more.

    Anyway a help is requested for my issue as follow:

    I need to show in a c1flexgrid a column that contains a Picture saved in the database (SQL-Server). Is there please a simple way to Show the Picture in the record set with VS2013 Visual Basic not C#.

    Thanks a lot

    SN

  • Posted 16 January 2018, 6:25 pm EST

    Hi Said!

    Yes, it’s been time that our website has changed. If you have any suggestion/feedback regarding this, much appreciated.

    And, I am attaching a sample to this post, that meets your requirements. Kindly refer the same.

    Prj_PictureColumn.zip

    Best,

    Meenakshi

  • Posted 16 January 2018, 9:32 pm EST

    Dear Meenakshi,

    Thanks for the quick reply. I will do, if i have any suggestion.

    Regarding your sample is great and working. I tried to adjust it to my own function. Unlikely it is not working. I do not know waht i am missing. I have joind to your Project a new Form2 where you can see the code how i fill in the C1Flexgrid. I also included a small MDB with one table for the Trial. Generally i work with the SQL-Server.

    Waiting for your Feedback.

    Best regards

    Said

  • Posted 17 January 2018, 5:35 pm EST

    Said!

    It should not cause problem till the time code execution flow is being maintained. Can you please show me the modified sample, for investigating it at my end?

    Best regards,

    Meenakshi

  • Posted 17 January 2018, 8:31 pm EST

    Dear Meenakshi,

    I have joined all in a zip file. Please see Form2.

    again attached the zip file.

    The following is the code of Form2:

    Imports System.Data.OleDb

    'C1

    Imports C1.Win.C1FlexGrid

    Public Class Form2

    Dim SqlStr As String = “”

    Private OleDbdataSet As DataSet

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            DGLaden()
    
        Catch ex As Exception
            MsgBox("Error 'Form2_Load' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Private Sub DGLaden()
        Try
            C1FlexGrid1.DataSource = Nothing
            C1FlexGrid1.Rows.RemoveRange(1, C1FlexGrid1.Rows.Count - 1)
    
            SqlStr = "SELECT ID, showImage, statuseng FROM status ORDER BY ID"
            Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\tImage.mdb;User Id=admin;Password=;"
            Dim OleDbda As New OleDb.OleDbDataAdapter(SqlStr, conn)
            OleDbdataSet = New DataSet
            OleDbda.Fill(OleDbdataSet, "IListe")
            Dim dt As DataTable = OleDbdataSet.Tables("IListe")
            C1FlexGrid1.DataSource = dt
            'Formatting C1FlexGrid
            With C1FlexGrid1
                .Cols(1).Caption = "ID"         'ID
                .Cols(1).Width = 100
    
                .Cols(2).Caption = "Image"      'showImage
                .Cols(2).Width = 200
    
                .Cols(3).Caption = "Text"       'statuseng
                .Cols(3).Width = 180
            End With
    
            C1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw
            AddHandler C1FlexGrid1.OwnerDrawCell, AddressOf C1FlexGrid1_OwnerDrawCell
    
        Catch ex As Exception
            MsgBox("Error 'DGLaden' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Sub C1FlexGrid1_OwnerDrawCell(sender As Object, e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs)
        Try
            If (e.Col).Equals(2) AndAlso e.Row > 0 Then
                e.Image = GetImageFromByteArray(TryCast(C1FlexGrid1(e.Row, e.Col), [Byte]()))
                e.Style.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.Stretch
                e.Style.Display = C1.Win.C1FlexGrid.DisplayEnum.ImageOnly
            End If
    
        Catch ex As Exception
            MsgBox("Error 'C1FlexGrid1_OwnerDrawCell' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Private Function GetImageFromByteArray(picData As Byte()) As Image
        Try
            If (picData) Is Nothing Then
                Return Nothing
            End If
    
            '' is this is an embedded object
            Dim bmData As Integer = If((picData(0) = 21 AndAlso picData(1) = 28), 78, 0)
    
            '' load the picture    
            Dim img As Image = Nothing
            Try
                Dim ms As New System.IO.MemoryStream(picData, bmData, picData.Length - bmData)
                img = Image.FromStream(ms)
            Catch
            End Try
            '' return what we got  
    
            Return img
    
        Catch ex As Exception
            MsgBox("Error 'GetImageFromByteArray' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Function
    

    End Class

    Best regards

    Na

  • Posted 17 January 2018, 8:35 pm EST

    Dear Meenakshi,

    It seems the drag and drop of attachment (Zip file with 469k) is not working. anyway i send to you the code in an easy way.

    Best regards

    Na

  • Posted 18 January 2018, 3:54 pm EST

    Hi Said!

    You just need to set WidthDisplay property of the image column to some desired value.

    C1FlexGrid1.Cols(*ImageColumnIndex*).WidthDisplay = 100
    

    It should show the images in grid’s column.

    “It seems the drag and drop of attachment (Zip file with 469k) is not working.”

    • There might be some issue with it. Please let me know the error message you get while trying drag-drop.

    Best,

    Meenakshi

  • Posted 18 January 2018, 9:16 pm EST

    Hi Meenakshi,

    Unlikely not. Zip attached and Following the updated code:

    Imports System.Data.OleDb

    'C1

    Imports C1.Win.C1FlexGrid

    Public Class Form2

    Dim SqlStr As String = “”

    Private OleDbdataSet As DataSet

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            DGLaden()
    
        Catch ex As Exception
            MsgBox("Error 'Form2_Load' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Private Sub DGLaden()
        Try
            C1FlexGrid1.DataSource = Nothing
            C1FlexGrid1.Rows.RemoveRange(1, C1FlexGrid1.Rows.Count - 1)
    
            SqlStr = "SELECT ID, showImage, statuseng FROM status ORDER BY ID"
            Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\tImage.mdb;User Id=admin;Password=;"
            Dim OleDbda As New OleDb.OleDbDataAdapter(SqlStr, conn)
            OleDbdataSet = New DataSet
            OleDbda.Fill(OleDbdataSet, "IListe")
            Dim dt As DataTable = OleDbdataSet.Tables("IListe")
            C1FlexGrid1.DataSource = dt
            'Formatting C1FlexGrid
            With C1FlexGrid1
                .Cols(1).Caption = "ID"         'ID
                .Cols(1).Width = 100
    
                .Cols(2).Caption = "Image"      'showImage
                '.Cols(2).Width = 200
                .Cols(2).WidthDisplay = 200
    
                .Cols(3).Caption = "Text"       'statuseng
                .Cols(3).Width = 180
            End With
    
            C1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw
            AddHandler C1FlexGrid1.OwnerDrawCell, AddressOf C1FlexGrid1_OwnerDrawCell
    
        Catch ex As Exception
            MsgBox("Error 'DGLaden' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Sub C1FlexGrid1_OwnerDrawCell(sender As Object, e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs)
        Try
            If (e.Col).Equals(2) AndAlso e.Row > 0 Then
                e.Image = GetImageFromByteArray(TryCast(C1FlexGrid1(e.Row, e.Col), [Byte]()))
                'e.Style.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.Stretch
                e.Style.Display = C1.Win.C1FlexGrid.DisplayEnum.ImageOnly
            End If
    
        Catch ex As Exception
            MsgBox("Error 'C1FlexGrid1_OwnerDrawCell' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Private Function GetImageFromByteArray(picData As Byte()) As Image
        Try
            If (picData) Is Nothing Then
                Return Nothing
            End If
    
            '' is this is an embedded object
            Dim bmData As Integer = If((picData(0) = 21 AndAlso picData(1) = 28), 78, 0)
    
            '' load the picture    
            Dim img As Image = Nothing
            Try
                Dim ms As New System.IO.MemoryStream(picData, bmData, picData.Length - bmData)
                img = Image.FromStream(ms)
            Catch
            End Try
            '' return what we got  
    
            Return img
    
        Catch ex As Exception
            MsgBox("Error 'GetImageFromByteArray' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Function
    

    End Class

    Best regards

    Said

    tImage.zip

  • Posted 22 January 2018, 11:38 pm EST

    Hello Said,

    Thank you for modifying & sharing the sample application.

    Once we modified the connection string in the application you shared to match as per our database, we were able to see the images in grid, as shown below:



    However, as you can notice the images loaded in grid are getting trimmed, this is because the size of the image is greater than the size of the corresponding cells. In your case, images might be different and hence looking as if they are not loaded.

    To resolve this, add ```

    e.Style.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.Stretch

    _C1FlexGrid1.Rows.DefaultSize = 100

    Following the above suggestions, you should then be able to see the images loaded properly in grid, as shown below
    [img]public\uploads\460082f663a96ac00b3904215244b8851516710921678.png[/img]
    
    Thanks,
    Ruchir Agarwal
  • Posted 23 January 2018, 11:21 pm EST

    Dear Agarwal,

    I really appreciate your help, unfortunitly i still cannot see the Pictures, Folling is the code including your suggestions:

    Public Sub DGBLoad()

    Try

    Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor

        DGB.DataSource = Nothing
        DGB.Rows.RemoveRange(1, DGB.Rows.Count - 1)
    
        'ODBC
        SqlStr = "SELECT status.StateIcon, Charge.ChargenNr, status.statuseng, status.status_id, Charge.AuftragNr FROM Charge LEFT JOIN status ON Charge.Status = status.status_id " & _
                 "WHERE Charge.AuftragNr = '" & GetString(Glb_POno) & "' ORDER BY Charge.ChargenNr"
        Dim Odbcda As New Odbc.OdbcDataAdapter(SqlStr, connectionString)
        odbcDataSet = New DataSet
        Odbcda.Fill(odbcDataSet, "DGBList")
        Dim dt As DataTable = odbcDataSet.Tables("DGBList")
        DGB.DataSource = dt
        With DGB
            .Cols(0).Visible = False
    
            .Cols(1).Caption = "Picture"    
            .Cols(1).WidthDisplay = 200
    
            .Cols(2).Caption = "Bundle no"
            .Cols(2).Width = 200
    
            .Cols(3).Caption = "State"       
            .Cols(3).Width = 200
    
            .Cols(4).Width = 0             
            .Cols(5).Width = 0             
        End With
    
    
        DGB.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw
        AddHandler DGB.OwnerDrawCell, AddressOf DGB_OwnerDrawCell
    
        DGB.Rows.DefaultSize = 100
    
        DGB.Rows(1).Selected = False
    
        Windows.Forms.Cursor.Current = Windows.Forms.Cursors.Default
    
    Catch ex As Exception
        MsgBox("Error 'DGBLoad' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
    Finally
        Windows.Forms.Cursor.Current = Windows.Forms.Cursors.Default
    End Try
    

    End Sub

    Sub DGB_OwnerDrawCell(sender As Object, e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs)

    Try

    If (e.Col).Equals(1) AndAlso e.Row > 0 Then

    e.Image = GetImageFromByteArray(TryCast(DGB(e.Row, e.Col), Byte))

    e.Style.ImageAlign = C1.Win.C1FlexGrid.ImageAlignEnum.Stretch

    e.Style.Display = C1.Win.C1FlexGrid.DisplayEnum.ImageOnly

    End If

    Catch ex As Exception
        MsgBox("Error 'DGB_OwnerDrawCell' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
    End Try
    

    End Sub

    Private Function GetImageFromByteArray(picData As Byte()) As Image

    Try

    If (picData) Is Nothing Then

    Return Nothing

    End If

        '' is this is an embedded object
        Dim bmData As Integer = If((picData(0) = 21 AndAlso picData(1) = 28), 78, 0)
    
        '' load the picture    
        Dim img As Image = Nothing
        Try
            Dim ms As New System.IO.MemoryStream(picData, bmData, picData.Length - bmData)
            img = Image.FromStream(ms)
        Catch
        End Try
        '' return what we got  
    
        Return img
    
    Catch ex As Exception
        MsgBox("Error 'GetImageFromByteArray' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
    End Try
    

    End Function

    Best regards

    Said Nai

  • Posted 24 January 2018, 8:17 pm EST

    Hello Said,

    Upon investigating on this issue, it seems there is some issue with the *.mdb file you are using as even other controls like MS-PictureBox, C1PictureBox were also not able to load the image.

    However, since MS-DataGridView was able to load the images properly, I have forwarded the case to the concerned development team for investigation {Tracking ID: 305992}.

    Meanwhile, one workaround we could find: Upon editing the image like changing its resize in image editor and then trying to load them fixed the problem and the images could be seen properly in the grid. Please refer attached sample and try running the modified db.

    Thanks,

    Ruchir Agarwal

    LoadImage.zip

  • Posted 24 January 2018, 10:57 pm EST

    Dear Agarwal,

    Please note that in my Project i do not use (*.MDB). I use the SQL-Server database.

    Best regards

    Na

  • Posted 14 February 2018, 12:35 am EST

    Dear Agarwal,

    Any news about this issue_

    Best regards

    Said

  • Posted 14 February 2018, 4:33 pm EST

    Hello Said,

    ETA of the fix to this issue is 2018v2 release.

    ~Ruchir Agarwal

  • Posted 14 February 2018, 6:27 pm EST

    Hello Agarwal,

    Please let me know per email. I have a similar function running with the MS standard Datagrid but i would like to use in the hole application the C1Flexgrid.

    Said

  • Posted 15 February 2018, 8:41 pm EST

    Hi Said,

    Thank you for showing further interest in using our controls in your application.

    I will make sure to come back to you when this is fixed.

    Best Regards,

    ~Ruchir

  • Posted 4 March 2018, 9:13 pm EST

    Hello Said,

    For resolving this issue, please use the following code:```

    Dim img As Image = (New ImageConverter()).ConvertFrom(TryCast(C1FlexGrid1(e.Row, e.Col), Byte))

    e.Image=img

    Regards,
    Ruchir Agarwal
  • Posted 10 March 2018, 9:28 pm EST

    Hello Said,

    Pardon me, I missed that point. In that case, the workaround would not suffice for you and we will have to wait for the developers’s response over this issue.

    I have pinged the concerned person for any updates [ID: 305992]. I will update with you the information they share with us.

    Thanks,

    Ruchir Agarwal

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels