Solved

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

Posted on 2011-03-02
4
986 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Advice on Xojo as a development tool over VB. 4 55
Get hold of longitude and latitude in iframe string 11 46
Creating a route in asp.net webforms 2 27
vb.net help 4 31
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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