Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Programmatically add rows to a bound Datagridview

Posted on 2011-03-13
Medium Priority
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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

604 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