Solved

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

Posted on 2010-11-13
4
978 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 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