image in the grid

2 Answers 65 Views
GridView
Laurent
Top achievements
Rank 1
Iron
Iron
Laurent asked on 14 Aug 2024, 07:55 AM

Objective: get the icon of a file (word,excel,pdf,...) 

In L4G, I use the ExtractAssociatedIconA function to obtain a handle for an icon stored as a resource in a file or an icon stored in the executable file associated with a file.
And I associate it with a button using the SendMessageA function.

Example code:
  DEFINE VARIABLE FILENAME AS CHARACTER NO-UNDO.        
  DEFINE VARIABLE IconInd AS INTEGER NO-UNDO.
  DEFINE VARIABLE hIcon AS INTEGER NO-UNDO.
  DEFINE VARIABLE iOldIcon AS INTEGER NO-UNDO.

  DO WITH FRAME {&FRAME-NAME}: 
    IF NUM-ENTRIES(RowObject.com_cha:SCREEN-VALUE,CHR(9)) > 0 THEN
      DO i = 1 TO NUM-ENTRIES(RowObject.com_cha:SCREEN-VALUE,CHR(9)):
          VIEW hbutton[i] hfillin[i].
          hbutton[i]:SELECTABLE = TRUE.
          ASSIGN DisplayName = ENTRY(i,RowObject.com_cha:SCREEN-VALUE,CHR(9)).
          IF Filename <> "" THEN
          DO:
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT NomFich,INPUT-OUTPUT IconInd,OUTPUT hIcon).
            RUN SendMessageA( hButton[i]:HWND, {&BM_SETIMAGE}, {&IMAGE_ICON}, 0, OUTPUT iOldIcon).
            RUN SendMessageA( hButton[i]:HWND, {&BM_SETIMAGE}, {&IMAGE_ICON}, hIcon, OUTPUT iOldIcon).
          END.

I want to do the same thing with a RadGridView? I've added an ima column as type GridViewImageColumn and in the : 
METHOD PRIVATE VOID GridLien_CellFormatting I'd like to put the image 
RUN SendMessageA( e:CellElement:?????? {&BM_SETIMAGE}, {&IMAGE_ICON}, hIcon, OUTPUT iOldIcon).
Can you help me?
Thank you

2 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 14 Aug 2024, 10:54 AM

Hello, Laurent,

GridViewImageColumn  displays images for database columns of Image data type. If you are creating GridViewImageColumn, and you have a whole column with images, I would recomend you to specify the FieldName property to which the column is bound. More useful information is available here: GridViewImageColumn - WinForms GridView Control - Telerik UI for WinForms

Alternatively, you can set image in CellFormatting event of RadGridView, passing the desired image to e.CellElement.Image:

void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.ColumnIndex == 3)
    {
        e.CellElement.Image = Resources.test;
    }
    else
    {
        e.CellElement.ResetValue(LightVisualElement.ImageProperty, Telerik.WinControls.ValueResetFlags.Local);
    }
}

I hope this information helps. If you have any other questions, please let me know. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Laurent
Top achievements
Rank 1
Iron
Iron
commented on 14 Aug 2024, 12:16 PM | edited

Thank you for your feedback,
But I don't think we understand.
Using:
e:CellElement:value = Image:FromFile(clsSession:RepDir + ‘\gui\icone\taskC-16.png’).                                         
It works but I use ExtractAssociatedIconA and SendMessageA and the 1st INPUT of SendMessageA is a HWND

Here is my code:
IF e:CellElement:ColumnInfo:name EQ ‘ima_lien’ AND e:CellElement:value EQ ? THEN DO:        
            
            cFile= ttEtuLien.rep_lien + ttEtuLien.fic_lien + ‘.’ + ttEtuLien.ext_lien.     
          .    
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT cFile,INPUT-OUTPUT IconInd,OUTPUT hIcon).  
                
            RUN SendMessageA( HERE YOU NEED xxxx:HWND, {&BM_SETIMAGE}, {&IMAGE_ICON}, hIcon, OUTPUT iOldIcon).
         
        END.
Nadya | Tech Support Engineer
Telerik team
commented on 15 Aug 2024, 11:57 AM

Hello, Laurent,

If I understand your case correctly, you use ExtractAssociatedIconA function to gets a handle to an icon stored as a resource. Then, you need to use this icon in cells in GridViewImageColumn. Please correct me if I am wrong. 

Note, RadGridview can display images which are Bitmap. The possible solution that I can suggest is to construct a new BitMap from a file, and the pass it to the cells in grid. You can use Bitmap.FromHicon() method to create a Bitmap from a Windows handle to an icon. Feel free to research any other solutions that might be suitable for your specific case to extract the icon.

Here is how you can assign a BitMap to cells in CellFormatting event:

void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.RowIndex == count - 1 && e.ColumnIndex == header)
    {
        e.CellElement.Image = new Bitmap(Resources.test);
    }
}

I hope this information is useful. Please let me know if you have other questions. 

Laurent
Top achievements
Rank 1
Iron
Iron
commented on 21 Aug 2024, 07:58 AM

Merci pour les informations.

J'ai réussi.

DEFINE VARIABLE oImageGrid      AS Bitmap NO-UNDO.
DEFINE VARIABLE oIntPtr         AS IntPtr NO-UNDO.

 

METHOD PRIVATE VOID GridLien_CellFormatting( INPUT sender AS System.Object, INPUT e AS Telerik.WinControls.UI.CellFormattingEventArgs ):
DEFINE VARIABLE cFichier    AS CHARACTER NO-UNDO.
DEFINE VARIABLE IconInd     AS INTEGER NO-UNDO.
        DEFINE VARIABLE hIcon       AS INTEGER NO-UNDO. 
        DEFINE VARIABLE iRow        AS INTEGER NO-UNDO.
        DEFINE VARIABLE hImaHwnd    AS INT64  NO-UNDO.
        
        DEFINE VARIABLE cRepLien AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cFicLien AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cExtLien AS CHARACTER NO-UNDO.
        
        IF NOT CAN-FIND (FIRST ttEtuLien) THEN RETURN.        
        IF ttEtuLien.cle_lien EQ 0 THEN RETURN.
        
        iRow = THIS-OBJECT:GridLien:CurrentRow:Index.        
        
        ASSIGN iRow = e:CellElement:RowIndex.
      
        IF iRow LT 0 OR iRow GT GridLien:ChildRows:Count - 1 THEN RETURN.        
                    
        IF TYPE-OF(e:CellElement,GridImageCellElement)  THEN DO:
                        
            cRepLien = GridLien:Rows:item[iRow]:Cells:Item["rep_lien"]:value:ToString().
            cFicLien = GridLien:Rows:item[iRow]:Cells:Item["fic_lien"]:value:ToString().
            cExtLien = GridLien:Rows:item[iRow]:Cells:Item["ext_lien"]:value:ToString().               
            cFichier= cRepLien + cFicLien + '.' + cExtLien.
            
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT cFichier,INPUT-OUTPUT IconInd,OUTPUT hIcon). 
                        
            oIntPtr= NEW IntPtr(hIcon).            
            oImageGrid = Bitmap:FromHicon(oIntPtr).                        
            e:CellElement:image = NEW Bitmap(oImageGrid).
            
            RETURN.                                         
        END.
        
RETURN.

END METHOD.
Nadya | Tech Support Engineer
Telerik team
commented on 21 Aug 2024, 09:02 AM

Hello, Laurent,

I am glad that the suggested solution helps you to achieve your goal. Thank you for sharing your code snippet.

Do not hesitate to contact us if you have other questions.

0
Laurent
Top achievements
Rank 1
Iron
Iron
answered on 13 Feb 2025, 10:08 AM

Hello Nadya,

I'm a new question.

I want generate à bitmap not use the METHOD PRIVATE VOID Grid_CellFormatting(......)

i want update the bitmat use a another methode, but my problem is why use a CellElement .

i tried this.... but nothing

if you have a solution ?

Best regards.

ex code:

in first:

i run the methode 'initEcran'

METHOD PUBLIC VOID InitEcran( INPUT ipcTypeEcran AS CHARACTER, INPUT-OUTPUT TABLE FOR ttEtu ):
        DEFINE VARIABLE iRow AS INTEGER NO-UNDO.
               
        //Initialise code devise
        RUN InitialiseCodeDevise.

        //Charge Code devise
        ChargeCodeDevise('InitBe').

        Affiche(INPUT-OUTPUT TABLE ttEtu).

        //initialise the business entity
        InitialisationBeEtuLien().
        
        glFlgInitEcran = NO.
        gcInitialDir   = clsSession:RepUSr + "\export".
        
        BtnModifier:ENABLED  = CAN-FIND(FIRST ttEtuLien).
        BtnSupprimer:ENABLED = CAN-FIND(FIRST ttEtuLien).
        BtnOuvrir:ENABLED    = CAN-FIND(FIRST ttEtuLien).
        
        iRow= 0.
        EMPTY TEMP-TABLE ttCleEtuLien.
           
        FOR EACH ttEtuLien NO-LOCK:
            IF iRow EQ 0 THEN DO:                            
              CREATE ttCleEtuLien.
              ASSIGN ttCleEtuLien.cle_cha  = ttEtuLien.cle_cha
                     ttCleEtuLien.cle_lien = ttEtuLien.cle_lien
                     ttCleEtuLien.ext_lien = ttEtuLien.ext_lien
                     ttCleEtuLien.fic_lien = ttEtuLien.fic_lien
                     ttCleEtuLien.rep_lien = ttEtuLien.rep_lien
                     .
            END.
                   
            AjoutIconLien(ttEtuLien.rep + ttEtuLien.fic_lien + ttEtuLien.ext_lien, iRow).
            iRow= iRow + 1.            
        END.
         
        GridLien:TableElement:RowHeight = 36.
                
        RETURN.

    END METHOD.

 

 

METHOD PUBLIC VOID AjoutIconLien( INPUT ipcFichier AS CHARACTER, INPUT ipiRow AS INTEGER ):
               
        DEFINE VARIABLE IconInd  AS INTEGER   NO-UNDO.
        DEFINE VARIABLE hIcon    AS INTEGER   NO-UNDO. 
        DEFINE VARIABLE iRow     AS INTEGER   NO-UNDO.
        DEFINE VARIABLE hImaHwnd AS INT64     NO-UNDO.
        
        DEFINE VARIABLE oBitmap             AS Bitmap NO-UNDO.
        DEFINE VARIABLE oIntPtr             AS IntPtr NO-UNDO.
        DEFINE VARIABLE oImageCellElement   AS Telerik.WinControls.UI.GridImageCellElement.
        DEFINE VARIABLE oCellElement        AS Telerik.WinControls.UI.GridCellElement.
        
            MESSAGE '==AjoutIconLien==' SKIP 
            ipcFichier SKIP ipiRow SKIP GridLien:ChildRows:Item[ipiRow]:Cells:Item["rep_lien"]:value:ToString() SKIP GridLien:ChildRows:Count
            
            VIEW-AS ALERT-BOX.
                   
            RUN ExtractAssociatedIconA(INPUT 0,INPUT-OUTPUT ipcFichier,INPUT-OUTPUT IconInd,OUTPUT hIcon). 
                        
            oIntPtr= NEW IntPtr(hIcon).            
            oBitmap = Bitmap:FromHicon(oIntPtr).   
        
            oCellElement = GridLien:TableElement:GetCellElement(GridLien:rows:Item[ipiRow], GridLien:Columns[0]). 
                        
            oImageCellElement= CAST(oCellElement ,GridImageCellElement).
                                       
            MESSAGE TYPE-OF(oImageCellElement,GridImageCellElement) SKIP            
            VIEW-AS ALERT-BOX.
                                                
            //oImageCellElement:image = NEW Bitmap(oBitmap).

        RETURN.
    END METHOD.
Nadya | Tech Support Engineer
Telerik team
commented on 18 Feb 2025, 07:49 AM

Hello, Laurent,

The CellFormatting event in grid is needed in such cases as your, to customize the cells by adding images, change text, colors, etc. Since RadGridView uses UI virtualization, its visual cells and rows are being reused. This means that in operations like scrolling up and down, the cell elements are being reused while the data for its cell changes accordingly to show all the rows that you have. This is why, we expose the CellFormatting event - it triggers for every visual cell to ensure that the correct formatting is applied every time. 

More information about the UI virtualization in RadGridViem and formatting events you can find here:

UI Virtualization - WinForms GridView Control - Telerik UI for WinForms
Formatting Cells - WinForms GridView Control - Telerik UI for WinForms

So, in case you need to assign images in cells it is necessary to handle the CellFormatting event as discussed in my previous replies.

I reviewed the provided code snippet that you submitted but it does not get very clear to me what exactly you need to achieve now. I assume that you are working in OpenEdge environment, please correct me if I am wrong. We provide support for Telerik UI for WinForms only in C# and VB. If you have any specific questions regarding the OpenEdge environment you can ask for help here: Using Telerik UI for WinForms with OpenEdge - Progress Community. If you are working in a different environment, help and solutions can likely be found there.

I hope this information is useful for you. Please let me know if I can assist you further.

Tags
GridView
Asked by
Laurent
Top achievements
Rank 1
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Laurent
Top achievements
Rank 1
Iron
Iron
Share this question
or