Solved

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

Posted on 2011-03-02
4
996 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
ID: 35023816
Try using the CellEndEdit event of the datagridview instead
0
 
LVL 1

Author Comment

by:alsam
ID: 35024688

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
ID: 35032774
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
ID: 35034170
Thank you very much...
It works with small adjustments...
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …

733 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