Results 1 to 4 of 4

Thread: image to cell datagridview

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    9

    image to cell datagridview

    Hi everyone,
    I need to insert a green / red up / down arrow in the datagridview cells as well as the numeric value, depending on the cell value.
    Searched the net, found these two subs working perfectly.
    I did not understand how it works, I just understood how to change the symbol and according to the value.
    I would like to better understand how the Sub works also because I can't change the font (Size and bold of the cell where I go to match the arrow
    thank you all.


    Private Sub DataGridView_CellPainting(ByVal pcolumn As Integer, ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEvent Args)

    '------------------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------------------
    '------- QUESTA SABRUTIN SERVE PER METTERE LE FRECCE E LA STRINGA IN UNA UNICA CELLA DEL DATAGRIDVIEW ------
    '------------------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------------------

    Dim zsignimage As Image = Nothing
    Dim format As New StringFormat()
    If e.ColumnIndex = pcolumn AndAlso e.RowIndex > -1 Then


    Select Case Replace(e.Value, "%", "")

    Case 0
    zsignimage = ImageList1.Images.Item(4)
    Case Is = "0,00"
    zsignimage = ImageList1.Images.Item(4)
    Case Is = "NaN"
    zsignimage = ImageList1.Images.Item(4)
    Case Is > 0
    zsignimage = ImageList1.Images.Item(1)
    Case Is < 0
    zsignimage = ImageList1.Images.Item(2)
    End Select


    e.PaintBackground(e.ClipBounds, True) 'transparant in cursor
    'draw icon image
    format.LineAlignment = StringAlignment.Center
    format.Alignment = StringAlignment.Near
    Dim x As Integer = (e.CellBounds.Width / 4)
    Dim iconRect = New Rectangle(e.CellBounds.Left + 7, e.CellBounds.Top + 1, x - 7, e.CellBounds.Height - 4)
    e.Graphics.DrawImage(zsignimage, iconRect)
    'draw text
    format.LineAlignment = StringAlignment.Center
    format.Alignment = StringAlignment.Far
    Dim textRect = New Rectangle(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Width, e.CellBounds.Height)
    e.Graphics.DrawString(e.Value, Font, Brushes.Black, textRect, format)
    e.CellStyle.SelectionBackColor = Color.Transparent
    e.Handled = True
    End If
    End Sub


    Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    ' '----------------------------------------------------------------------------------------------------------------------------------
    ' '----------------------------------------------------------------------------------------------------------------------------------
    ' '------- QUESTA SABRUTIN SERVE PER METTERE LA FRECCIA Nella Colonna scelta E LA STRINGA IN UNA UNICA CELLA DEL DATAGRIDVIEW ------
    ' '----------------------------------------------------------------------------------------------------------------------------------
    ' '----------------------------------------------------------------------------------------------------------------------------------
    DataGridView_CellPainting(3, sender, e) '3 is column index of "Change"
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: image to cell datagridview

    Hello Antonello2,

    Could you rephrase your question and post the relevant code enclosed by code tags? Use the # button in the toolbar to add these tags to your post.

    yours,
    Peter Swinkels
    Last edited by Peter Swinkels; Jan 27th, 2021 at 03:22 AM. Reason: fixed grammar

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: image to cell datagridview

    on this line :
    Code:
    e.Graphics.DrawString(e.Value, Font, Brushes.Black, textRect, format)
    you set the font by a variable named "font".

    You can set your own :
    Code:
    dim font_new as New Font(String, Single, FontStyle) ' example Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold)
     e.Graphics.DrawString(e.Value, Font_new, Brushes.Black, textRect, format)
    more info here : https://docs.microsoft.com/fr-fr/dot...t-plat-ext-5.0

    have a look also here to understand the drawstring method : https://docs.microsoft.com/fr-fr/dot...t-plat-ext-5.0
    Last edited by Delaney; Jan 26th, 2021 at 04:32 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    9

    Re: image to cell datagridview

    thank you so much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width