Solved

Programmatically add rows to a bound Datagridview

Posted on 2011-03-13
3
586 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 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

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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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