Solved

Calculating column datagridview

Posted on 2014-02-17
3
498 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
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 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 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 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

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

Read about achieving the basic levels of HRIS security in the workplace.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

18 Experts available now in Live!

Get 1:1 Help Now