?
Solved

Programmatically add rows to a bound Datagridview

Posted on 2011-03-13
3
Medium Priority
?
592 Views
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]
0
Comment
Question by:windlust
[X]
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
3 Comments
 
LVL 1

Expert Comment

by:alsam
ID: 35123436
Hi,
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...
Alsam
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
 

Author Comment

by:windlust
ID: 35158750
Thanks,

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?
0
 
LVL 1

Accepted Solution

by:
alsam earned 2000 total points
ID: 35169294
Hi,
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
Alsam
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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

741 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