Calculate Sum and Percentage for each column individually in PDF

Last Reply one day ago By pandeyism

Posted 8 days ago

I have the following data 

SessionName

Term

Exam

AdmissionNo

Subject

Max

Mark

S-19

Term-1

A1-T1

R-01

English

40

23

S-19

Term-1

A1-T1

R-02

English

40

25

S-19

Term-1

A1-T1

R-01

Math

40

28

S-19

Term-1

A1-T1

R-02

Math

40

30

S-19

Term-1

A2-T1

R-01

English

60

43

S-19

Term-1

A2-T1

R-02

English

60

25

S-19

Term-1

A2-T1

R-01

Math

60

28

S-19

Term-1

A2-T1

R-02

Math

60

30

S-19

Term-2

A1-T2

R-01

English

40

33

S-19

Term-2

A1-T2

R-02

English

40

25

S-19

Term-2

A1-T2

R-01

Math

40

28

S-19

Term-2

A1-T2

R-02

Math

40

30

S-19

Term-2

A2-T2

R-01

English

60

53

S-19

Term-2

A2-T2

R-02

English

60

25

S-19

Term-2

A2-T2

R-01

Math

60

28

S-19

Term-2

A2-T2

R-02

Math

60

30

from this data following report is generted by using the following code

 

private void tbGetData_Click(object sender, EventArgs e)
        {
            DataTable dt = GetFromTable();
            this.dGV.DataSource = dt;
        }
        private DataTable GetFromTable()
        {

            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(@"SELECT SessionName,TermName,ExamType,AcademicName,ClassName,SectionName,SPic,fa.AdmissionNo,RollNo,SName,FName,FPhone,SubjectName,Max,Mark FROM tblSetMarks as fa
                                                inner join tblSession as ses on fa.SessionID=ses.SessionID
                                                inner join tblSetTerm as ste on fa.TermID=ste.TermID
                                                inner join tblSetExam as st on fa.SetExamID=st.SetExamID
                                                inner join tblStdReg as sr on fa.AdmissionNo=sr.AdmissionNo
                                                inner join tblAcademicYear as ay on sr.YearID=ay.YearID
                                                inner join tblDefClass as dc on sr.ClassID=dc.ClassID
                                                inner join tblDefSection as ds on sr.SectionID=ds.SectionID    
                                                inner join tblAssignSubjects as fh on fa.SubjectID=fh.ASID inner join tblDefSubject as df on fh.SubjectID=df.SubjectID
                                                group by SPic,SessionName,TermName,ExamType,AcademicName,ClassName,SectionName,fa.AdmissionNo,RollNo,SName,FName,FPhone,SubjectName,Max,Mark order by AdmissionNo,SubjectName asc;", con);            
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

to generate the report the code is

 

private void tbPageTwo_Click(object sender, EventArgs e)
        {
            string pdfpath = @"C:\Reports\";
            if (!Directory.Exists(pdfpath))
            {
                Directory.CreateDirectory(pdfpath);
            }

            string folderPath = @"C:\Reports\";
            FileStream stream = new FileStream(folderPath + "FinalResultCard-2 on " + DateTime.Now.ToShortDateString() + ".pdf", FileMode.Create);
            Document doc = new Document(PageSize.LEGAL);
            PdfWriter.GetInstance(doc, stream);
            doc.Open();

            DataTable sTable = GetFromTable();
            var grouped = from x in sTable.AsEnumerable()
                          group x by new { a = x["AdmissionNo"] } into g
                          select new
                          {
                              Value = g.Key,
                              ColumnValues = g
                          };

            DataTable dtfinal = null;
            foreach (var key in grouped)
            {
                dtfinal = sTable.Clone();

                foreach (var columnValue in key.ColumnValues)
                {
                    dtfinal.ImportRow(columnValue);
                }

                
                PdfPTable tableb = new PdfPTable(4);
                float[] widthim = new float[] { 0.1f, 0.1f, 0.1f, 0.05f };
                tableb.SetWidths(widthim);
                tableb.DefaultCell.Padding = 7f;
                tableb.HorizontalAlignment = Element.ALIGN_CENTER;
                tableb.TotalWidth = 550f;
                tableb.LockedWidth = true;
                tableb.SpacingBefore = 5f;
                tableb.SpacingAfter = 0f;
                PdfPCell header = new PdfPCell(new Phrase("Student Details", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12)));
                header.Indent = 10;
                header.HorizontalAlignment = 1;
                header.Padding = 5f;
                header.Colspan = 4;
                tableb.AddCell(header);

                PdfPTable nested = new PdfPTable(3);
                float[] widthi = new float[] { 0.1f, 0.1f, 0.1f };
                nested.SetWidths(widthi);
                nested.DefaultCell.Padding = 7f;
                nested.AddCell(new Phrase("Session: " + dtfinal.Rows[0]["SessionName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("Reg No. " + dtfinal.Rows[0]["AdmissionNo"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("" + dtfinal.Rows[0]["SName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("" + dtfinal.Rows[0]["FName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("AcademicYear: " + dtfinal.Rows[0]["AcademicName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("Class: " + dtfinal.Rows[0]["ClassName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("Section: " + dtfinal.Rows[0]["SectionName"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                nested.AddCell(new Phrase("Phone No. " + dtfinal.Rows[0]["FPhone"], FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));

                PdfPCell nesthousing = new PdfPCell(nested);
                nesthousing.Colspan = 3;
                nesthousing.Padding = 0f;
                tableb.AddCell(nesthousing);
                Byte[] StuPic = (Byte[])dtfinal.Rows[0]["SPic"];
                iTextSharp.text.Image Stuimage = iTextSharp.text.Image.GetInstance(StuPic);
                Stuimage.ScaleAbsolute(60f, 60f);
                PdfPCell bottom = new PdfPCell((Stuimage));
                bottom.Padding = 5f;
                bottom.HorizontalAlignment = 1;
                tableb.AddCell(bottom);
                doc.Add(tableb);
                var groupbySubject = from x in dtfinal.AsEnumerable()
                                     group x by new { a = x["SubjectName"] } into g
                                     select new
                                     {
                                         Value = g.Key,
                                         ColumnValues1 = g
                                     };

                DataTable dt2 = null;
                foreach (var item in groupbySubject)
                {
                    dt2 = dtfinal.Clone();
                    foreach (var columnValue in item.ColumnValues1)
                    {
                        dt2.ImportRow(columnValue);
                    }
                    
                    PdfPTable table11 = new PdfPTable(5);
                    string no1 = dt2.Rows[0]["SubjectName"].ToString();
                    table11.TotalWidth = 500f;
                    table11.LockedWidth = true;
                    float[] widths11 = new float[] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };
                    table11.SetWidths(widths11);
                    table11.DefaultCell.Padding = 3f;
                    table11.HorizontalAlignment = Element.ALIGN_CENTER;
                    table11.SpacingBefore = 5f;

                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {
                        // table1.AddCell(new Phrase(dt2.Rows[j]["ExamType"].ToString().Replace('-', '.'), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                    }

                    for (int j = 0; j < dt2.Rows.Count + 1; j++)
                    {
                        if (j == 1)
                        {
                            table11.AddCell(new Phrase(no1, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                            for (int k = 0; k < dt2.Rows.Count; k++)
                            {
                                table11.AddCell(new Phrase(dt2.Rows[k]["Mark"].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                            }
                        }

                    }

                    doc.Add(table11);

                }
                PdfPTable table111 = new PdfPTable(5);
                table111.TotalWidth = 500f;
                table111.LockedWidth = true;
                float[] widths111 = new float[] { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };
                table111.SetWidths(widths111);
                table111.DefaultCell.Padding = 3f;
                table111.HorizontalAlignment = Element.ALIGN_CENTER;
                table111.SpacingBefore = 5f;
                table111.AddCell(new Phrase("Obt", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("Max", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("%", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("Grade", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("Position", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                table111.AddCell(new Phrase("", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10)));
                doc.Add(table111);

this is generating report in the following format

fsdfsdfadsf

now i want to calculate the sum, percentage and grade for each 4 column individually. how to do it?

You are viewing reply posted by: smile 7 days ago.
Posted 7 days ago

sir i want output in the following format as per asked in my question

for R-01 it should like this

Subject     A1-T1        A2-T1      A1-T2   A2-T2

                 40             60             40        60

1-English     23            43            33         53

2-Math        28             28           28         28

Obtain         51           71           61         81

Max             80         120          80         120

%                 64          59.16     76.25      68

then in the next page

for R-02 it should like this

Subject     A1-T1        A2-T1      A1-T2   A2-T2

                 40             60             40        60

1-English     23            40            33         53

2-Math        27             40           33         28

Obtain         50           80           66         81

Max             80         120          80         120

%                63          67            83         68