Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all
here i use data grid view row post paint to set a row icons for my datagridview , for example:


VB
Private Sub DataGridView1_RowPostPaintRed( _
    ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) _
    Handles DataGridView1.RowPostPaint

VB
For Each row As DataGridViewRow In DataGridView1.Rows

VB
Dim myBitmap As New Bitmap(ImageList1.Images(0))
            Dim myIcon As Icon = Icon.FromHandle(myBitmap.GetHicon())
            Dim graphics As Graphics = e.Graphics
            Dim iconHeight As Integer = 14
            Dim iconWidth As Integer = 14
            Dim xPosition As Integer = e.RowBounds.X + (DataGridView1.RowHeadersWidth \ 2)
            Dim yPosition As Integer = e.RowBounds.Y + ((DataGridView1.Rows(e.RowIndex).Height - iconHeight) \ 2)
            Dim rectangle As New Rectangle(xPosition, yPosition, iconWidth, iconHeight)
            graphics.DrawIcon(myIcon, rectangle)


in this private sub i use my 1st image from image list (Red)


My question is :
how can i use that private subs in this sub :
VB
Private Sub myUpdate(ByVal str As String)
        Dim jo As JObject = JObject.Parse(str), temp As String, val As String
        For Each row As DataGridViewRow In DataGridView1.Rows
            temp = row.Cells(0).Value.ToString
            val = jo(temp)
            If row.Cells(1).Value < val Then
                '--
            Else
                ' call red private sub (above)

            End If
            row.Cells(1).Value = val
            row.Cells(2).Value = fractionPart(2, val)
        Next
    End Sub
Posted
Comments
Mehdi Gholam 16-Mar-15 12:33pm    
Change it to Public.
S.a.azizi 16-Mar-15 12:46pm    
:-/ No , i think i cant call this sub in another sub because i think this sub works automatically :-??
Maciej Los 16-Mar-15 13:05pm    
Automatically? How?

1 solution

You don't normally call event handler subs directly - you would create a method which both subs call instead. In this case though, you can't easily do that because your code uses the Graphics context in order to do the drawing. While you could create the context yourself (and Dispose it when you have finished or nasty things start happening) the best way to copy with this is to cause the system to raise the event: call the DataGridView.Invalidate method and it will force a re-paint, which will result in the row post-paint event being raised and your code executed automatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900