Programmatically add rows to a bound Datagridview

Posted on 2011-03-13
Last Modified: 2012-05-11
Every where I have looked indicates that you can not programmatically add rows to a bound datagridview.( I want to add total rows at the bottom of a grid that the datasource is being set = to a datatable.) Is this true? If so, what are other ideas for a work around?

[removed email from question title - webtubbs, ZA]
Question by:windlust
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

ID: 35123436
In order to add total row at the end of bound datagridview you can use CellPainting...
Here you can find an example how you could make this working for you...

Since I don't know the structure of your data you are loading into your datagridview you can play with column index to set from which colum you want to calculate totals....
In case below totals are calculated starting from column 3 (e.ColumnIndex > 1)...
Also, cell painting will provide your end users only with information about totals but will not be updated against your database in case you want to save changes (this is not bound to original data source...)...
Hope to help...
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


Author Comment

ID: 35158750

I use the cellpainting event for formatting columns but I could not understand how to call the cellpainting event and tell it to re-paint specific cells. Specifically I would like to call it from a Cell Changed event and pass over specific cell info to repaint. How would i do this?

Accepted Solution

alsam earned 500 total points
ID: 35169294
In order to refresh your totals after cell value changed I suggest you to use code bellowe I have just tested in order to bring solution to you...
I dont suggest you to try to call cellpainting....
Let m know if this is ok with your requirements...If not than we should try find different solution for you
Hope to help
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
                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 += Format(Convert.ToDouble(DataGridView1.Rows(i).Cells(e.ColumnIndex).Value), "0.00")
                End If
            Next i

        End If

        DataGridView1.Rows(DataGridView1.NewRowIndex).Cells(e.ColumnIndex).Value = sum
    End Sub

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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