Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculating column datagridview

Posted on 2014-02-17
3
Medium Priority
?
522 Views
Last Modified: 2014-02-21
Hello,

How do you calculate an entire column in a datagridview in vb.net ?
0
Comment
Question by:lincstech
[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
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1000 total points
ID: 39865184
Dim total As Integer
            For x As Integer = 0 To DataGridView1.Rows.Count - 1
                  total += CInt(DataGridView1.Item(3, x).Value)
            Next

Change the 3 by the column you want to calculate on, knowing that the first column is 0.
If you have decimals, change total to a Double and CInt to CDbl.
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 39865319
Or you can use a Linq query to get the results as follows.

' Get the table from the DataGridView
Dim table As DataTable = DataGridView1.DataSource
' To Sum the column you can use a Linq query
' In this part of the query, t.Field(Of Integer), change Integer to thecorrect data type for the column to be Sum'ed
' In this part of the query, ("ColumnName"), Change the ColumnName to the name of the column being Sum'ed.
' The variable results will contain the total
Dim results = (From t In table.AsEnumerable()
               Select t.Field(Of Integer)("ColumnName")).Sum(Function(t) t)

Open in new window

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39869750
hi..try this way..
 
decimal a=0, b=0, c=0;
 
for (int i = 0; i < (gridview1.Rows.Count); i++)  
           {
               a = Convert.ToDecimal(gridview1.Rows.Cells["Column Index"].Text.ToString());
               c = c + a; //storing total qty into variable

}
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

704 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