Solved

How to add a new row in datagridview in vb.net that calculates totals for columns

Posted on 2010-11-13
4
988 Views
Last Modified: 2012-05-10
Hi,
I have populated datagridview from sql server database...
Datagridview has 5 colums...
First column is varchar column type and the rest are float....
The datagridview counts 15 rows....
I would like to add 16 th row to show sum of each respective column...It should look like

TOTAL    Sum(column1)    Sum(Column2)  Sum (Column3)     Sum(Column4)

Can someone help me to make this working....
Thanks...
0
Comment
Question by:alsam
  • 2
4 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 34128830
try this

'insert your column
Dim NewColumn As New DataGridViewColumn
Form1.DataGridView1.Columns.Insert(16, NewColumn)
Dim ColumnsToAdd As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
For x = 0 To Form1.DataGridView1.RowCount - 1
    Form1.DataGridView1.Item(16, x).Value = SumColumns(ColumnsToAdd, x)
Next

'and alsoadd this function
Function SumColumns(ByVal columns As List(Of Integer), ByVal row As Integer) As Double

Dim q As Double = 0.0
For Each y As Integer In columns
    Try
        q = Form1.DataGridView1.Item(y, row).Value + q
    Catch ex As Exception
    End Try
Next
Return q

End Function

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 34128837
Actually, you have to remove the 16 from the list that calls the function.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 34129183
See code below:

For more datagridview usage check:
http://dotnetperls.com/datagridview-vbnet
'insert total row
Dim totalIndex As Integer = DataGridView1.Rows.Add()
'calculate totals
For x = 1 To Form1.DataGridView1.ColCount - 1
    Form1.DataGridView1.Rows.Item(totalIndex).Cells(x).Value = SumColumns(x)
Next

Function SumColumns(ByVal col As Integer) As Double

Dim q As Double = 0.0
For x = 0 To Form1.DataGridView1.RowCount - 2
    q = Form1.DataGridView1.Item(x).Cells(col).Value + q
Next
Return q

End Function

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34131209
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Allow User To Arrange Columns At Datagridview 3 27
get column names from table in vb.net 8 30
Winform Module - What is the ASP.Net equiv 2 23
vb.net and creating a class 5 17
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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