Solved

Programmatically add rows to a bound Datagridview

Posted on 2011-03-13
3
565 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
  • 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 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now