Link to home
Start Free TrialLog in
Avatar of alsam
alsam

asked on

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

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

Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Try using the CellEndEdit event of the datagridview instead
Avatar of alsam
alsam

ASKER


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
ASKER CERTIFIED SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alsam

ASKER

Thank you very much...
It works with small adjustments...