Image in RadGridview

2 Answers 354 Views
GridView
Udo
Top achievements
Rank 1
Iron
Udo asked on 03 Feb 2022, 11:02 AM

Hello,

i have a sql Database with a imagepath.

How can i show the image in a colum

datasource ist a string with a filepath in the database.

 

Thanks Udo

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Feb 2022, 11:16 AM

Hi, Udo,

If you want to load the images from a file path that is stored in the database, I believe that the following forum thread shows a sample approach that may fit your requirement:

https://www.telerik.com/forums/have-image-file-path-display-column-with-image 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Udo
Top achievements
Rank 1
Iron
answered on 04 Feb 2022, 08:35 AM | edited on 04 Feb 2022, 11:59 AM

Hi there,

Thanks for your help.

Since I'm unfortunately not a professional, I need help again.

The images are not displayed in the correct size.


    Private Sub rgvObjektdaten_CreateCell(sender As Object, e As GridViewCreateCellEventArgs) Handles MasterTemplate.CreateCell
        If TypeOf e.Row Is GridDataRowElement AndAlso TypeOf e.Column Is GridViewDataColumn AndAlso (CType(e.Column, GridViewDataColumn)).Name = "Bildpfad" Then
            e.CellType = GetType(CustomGridImageCellElement)
        End If

    End Sub

Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Imports Telerik.WinControls
Imports Telerik.WinControls.Primitives
Imports Telerik.WinControls.UI

Class SurroundingClass
    Private Sub radGridView1_CreateCell(ByVal sender As Object, ByVal e As GridViewCreateCellEventArgs)
        If TypeOf e.Row Is GridDataRowElement AndAlso TypeOf e.Column Is GridViewDataColumn AndAlso (CType(e.Column, GridViewDataColumn)).Name = "Bildpfad" Then
            e.CellType = GetType(CustomGridImageCellElement)
        End If
    End Sub

    Public Class CustomGridImageCellElement
        Inherits GridDataCellElement
        Private _imagePrimitive As ImagePrimitive
        Public Sub New(column As GridViewDataColumn, row As GridRowElement)
            MyBase.New(column, row)
        End Sub
        Protected Overrides Sub CreateChildElements()
            MyBase.CreateChildElements()
            _imagePrimitive = New ImagePrimitive()
            _imagePrimitive.Margin = New Padding(3)
            Me.Children.Add(_imagePrimitive)
            Me.AutoSizeMode = RadAutoSizeMode.WrapAroundChildren
        End Sub
        Protected Overrides Sub SetContentCore(value As Object)
            If Me.Value IsNot Nothing AndAlso Not Me.Value.Equals(DBNull.Value) AndAlso File.Exists(Me.Value.ToString()) Then
                _imagePrimitive.Image = Bitmap.FromFile(Me.Value.ToString())
            Else
                _imagePrimitive.Image = Nothing
            End If
        End Sub
    End Class
End Class


Thanks

Udo
Top achievements
Rank 1
Iron
commented on 07 Feb 2022, 07:41 AM

Hi there,

here's another oddity.

When I scroll, the images disappear from the cells and the path to the image appears.
Todor
Telerik team
commented on 09 Feb 2022, 07:23 AM

Hi, Udo,

I created a sample project that is based on the approach used in this forum post: https://www.telerik.com/forums/have-image-file-path-display-column-with-image.

In order to be able to see correctly the images we need to have adjusted row height:

Me.RadGridView1.TableElement.RowHeight = 60
After that, I ended up with a clipped image at the bottom:

To fix this I have replaced the ImagePrimitive in the custom cell with a LightVisualElement and set its ImageLayout to Zoom.
Now the images are fully visible in the cell. If you need to display bigger images, you can set the RowHeight to another value or set the AutoSizeRows property of RadGridView to True.

Here is the updated implementation of the custom image cell:

Public Class CustomGridImageCellElement
    Inherits GridDataCellElement

    Private _imageElement As LightVisualElement

    Public Sub New(ByVal column As GridViewDataColumn, ByVal row As GridRowElement)
        MyBase.New(column, row)
    End Sub

    Protected Overrides Sub CreateChildElements()
        MyBase.CreateChildElements()

        _imageElement = New LightVisualElement()
        _imageElement.DrawImage = True
        _imageElement.Margin = New Padding(3)
        _imageElement.ImageLayout = ImageLayout.Zoom
        Me.Children.Add(_imageElement)
        Me.AutoSizeMode = RadAutoSizeMode.WrapAroundChildren
    End Sub

    Protected Overrides Sub SetContentCore(ByVal value As Object)
        If Me.Value IsNot Nothing AndAlso Not Me.Value.Equals(DBNull.Value) AndAlso File.Exists(Me.Value.ToString()) Then
            _imageElement.Image = Bitmap.FromFile(Me.Value.ToString())
        Else
            _imageElement.Image = Nothing
        End If
    End Sub
End Class
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Udo
Top achievements
Rank 1
Iron
commented on 11 Feb 2022, 06:16 AM

Thanks!!!

This works perfect.

 

Tags
GridView
Asked by
Udo
Top achievements
Rank 1
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Udo
Top achievements
Rank 1
Iron
Share this question
or