Solved

How to call cellPainting sub after cell value is changed in datagridview

Posted on 2011-03-02
4
970 Views
Last Modified: 2012-05-11
Hi,
I use CellPainting to show column total in datagridview...
When I load my form totals are calculated OK...
After I change content of cell in datagridview I expect totals to be changed but unfortunatelly it does not happens....It's recalculated when I change for eg width  of column where cell value is changed
How can I call cellPainting to recalculate totals after cell value is changed....

Please help

Thanks...

Code below...
Public Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

        Dim sum As Decimal = 0.0
        If (e.RowIndex = DataGridView1.NewRowIndex And e.ColumnIndex > 1) Then
            For i As Integer = 0 To DataGridView1.NewRowIndex - 1
                If (DataGridView1.Rows(i).Cells(e.ColumnIndex).Value.ToString().Trim() <> "") Then
                    sum += Convert.ToDecimal(DataGridView1.Rows(i).Cells(e.ColumnIndex).Value)
                End If
            Next i

            e.PaintBackground(e.CellBounds, False)
            e.Graphics.DrawString(sum.ToString(), DataGridView1.Font,
 Brushes.Black, e.CellBounds.Left + 2, e.CellBounds.Top + 2)
            e.Handled = True
        End If
    End Sub

Open in new window

0
Comment
Question by:alsam
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
Try using the CellEndEdit event of the datagridview instead
0
 
LVL 1

Author Comment

by:alsam
Comment Utility

Hi,
thank you for your reply...
Errors are as follows:
'PaintBackground' is not member of System.Windows.Forms.DataGridViewCellEventArgs
'Handled' is not member of System.Windows.Forms.DataGridViewCellEventArgs
'Graphics' is not member of System.Windows.Forms.DataGridViewCellEventArgs
......

Do you have any other idea what should I do?
Thanks
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
Comment Utility
Something like this do what you need?

Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles dataGridView1.CellEndEdit

Dim sum As Decimal = 0.0
        If (e.RowIndex = DataGridView1.NewRowIndex And e.ColumnIndex > 1) Then
            For i As Integer = 0 To DataGridView1.NewRowIndex - 1
                If (DataGridView1.Rows(i).Cells(e.ColumnIndex).Value.ToString().Trim() <> "") Then
                    sum += Convert.ToDecimal(DataGridView1.Rows(i).Cells(e.ColumnIndex).Value)
                End If
            Next i
           
        End If
        DataGridView1.Rows(NewRowIndex + 2).Cells(e.ColumnIndex + 2).Value = sum
End Sub


0
 
LVL 1

Author Closing Comment

by:alsam
Comment Utility
Thank you very much...
It works with small adjustments...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now