Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a datagridview with multiples rows from database.

The content of my datagrid can be printed, on every page can be printed only 45 record + 1 record for the SUM of the last tow columns.

How can i calculate the SUM for every 45 records? I don't know haw many record are in the database, can be 25 or 500.

OK,

I have the following table.

No. Column1 Column2 Column3 Column4
1 some data some data 20 30
2 some data some data 20 30
3 some data some data 20 30
................................................
45 some data some data 20 30
46 some data some data 20 30
................................................
89 some data some data 20 30
90 some data some data 20 30
................................................

To calculate the SUM of column3 for all rows i use:
C#
for (int i = 0; i < datagridview.Rows.Count; ++i)
            {
                suminchise += Convert.ToDouble(datagridview.Rows[i].Cells[3].Value);
            }

But, in my printing are displayed only 45 record on every page, and i need to calculate the sum of the column3 only for the first 45 row, and for page to for record 46 to 90, next 91 to 135, etc...
Posted
Updated 21-Oct-12 19:45pm
v2
Comments
Can you elaborate a little ? I am not clear about the requirement ?
Aciobanita Costel 21-Oct-12 12:30pm    
I populate my datagrid from a datatable. The content of my datagrid is printed in report, on a page i can display only 45 record from my datagridview, and in the bottom of the printed page a want to display the SUM of a column only for the record in that page. On the next page are displayed the next 45 rows, and the end the sum for the record that are displayed on the second page, ETC. The number of the record isn't know, Can be 2 or 500,
SUM of a coulmn ??? I can't make this out.
Can you give one simple example what SUM you will show in the bottom row ?
Aciobanita Costel 22-Oct-12 1:44am    
OK,

I have the following table.

No. Column1 Column2 Column3 Column4
1 some data some data 20 30
2 some data some data 20 30
3 some data some data 20 30
................................................
45 some data some data 20 30
46 some data some data 20 30
................................................
89 some data some data 20 30
90 some data some data 20 30
................................................

To calculate the SUM of column3 for all rows i use:
for (int i = 0; i < datagridview.Rows.Count; ++i)
{
suminchise += Convert.ToDouble(datagridview.Rows[i].Cells[3].Value);
}
But, in my printing are displayed only 45 record on every page, and i need to calculate the sum of the column3 only for the first 45 row, and for page to for record 46 to 90, next 91 to 135, etc...
Have you done paging to your DataGridView ?
If yes, what is the pagesize ?

1 solution

C#
int a[]=new int[7];
for (int i = 0; i < datagridview.Rows.Count; ++i)
            {
                  

                suminchise += Convert.ToDouble(datagridview.Rows[i].Cells[3].Value);
                if(i%45==0)
                {
                  a[j]=suminchise;
                  j++;
                  suminchise=0;
                }
                
            }


u can use a[0],a[1],.... as sum of every 45 record...and if sappose u have 100 records then u wil got
a[0]=sum of 1st 45 record
a[1]=sum of 2nd 45 record
and
suminchise=sum of rest 10 records;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900