ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Calculate Sum and Percentage for each column and Export to PDF in C# and VB.Net<p>Hi <a class="username" rel="smile"> smile</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string pdfpath = @"C:\Users\anand\Desktop\Test\\"; &nbsp;&nbsp;&nbsp; if (!Directory.Exists(pdfpath)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Directory.CreateDirectory(pdfpath); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; string folderPath = @"C:\Users\anand\Desktop\Test\\"; &nbsp;&nbsp;&nbsp; FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create); &nbsp;&nbsp;&nbsp; Document doc = new Document(); &nbsp;&nbsp;&nbsp; PdfWriter.GetInstance(doc, stream); &nbsp;&nbsp;&nbsp; doc.Open(); &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; dt.Columns.AddRange(new DataColumn[]{new DataColumn("SessionName",typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("Term",typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("Exam",typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("AdmissionNo",typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("Subject",typeof(string)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("Max",typeof(int)), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new DataColumn("Mark",typeof(int))}); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "English", 40, 23); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Math", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Urdu", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Science", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-01", "Computer", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "English", 60, 43); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Math", 60, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Urdu", 60, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Science", 60, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-01", "Computer", 60, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "English", 40, 33); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Math", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Urdu", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Science", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-01", "Computer", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "English", 40, 25); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Math", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Urdu", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Science", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-01", "Computer", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "English", 40, 25); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Math", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Urdu", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Science", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T1", "R-02", "Computer", 40, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "English", 60, 25); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Math", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Urdu", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Science", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-1", "A1-T2", "R-02", "Computer", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "English", 40, 53); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Math", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Urdu", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Science", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T1", "R-02", "Computer", 40, 28); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "English", 60, 25); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Math", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Urdu", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Science", 60, 30); &nbsp;&nbsp;&nbsp; dt.Rows.Add("S-19", "Term-2", "A2-T2", "R-02", "Computer", 60, 30); &nbsp;&nbsp;&nbsp; DataSet dsAdmissionNo = new DataSet(); &nbsp;&nbsp;&nbsp; DataView viewAdmissionNo = new DataView(dt); &nbsp;&nbsp;&nbsp; DataTable distinctAdmissionNoTable = viewAdmissionNo.ToTable(true, "AdmissionNo"); &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; distinctAdmissionNoTable.Rows.Count; i++) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dtAdmissionNo = dt.Select("AdmissionNo='" + distinctAdmissionNoTable.Rows[i][0] + "'").CopyToDataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataView viewExam = new DataView(dtAdmissionNo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable distinctExamTable = viewExam.ToTable(true, "Exam"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dtFinal = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtFinal.TableName = "tbl_" + distinctAdmissionNoTable.Rows[i][0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtFinal.Columns.Add("Subject"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; distinctExamTable.Rows.Count; j++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtFinal.Columns.Add(distinctExamTable.Rows[j][0].ToString(), typeof(int)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataView viewSubject = new DataView(dtAdmissionNo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable distinctSubject = viewSubject.ToTable(true, "Subject"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int k = 0; k &lt; distinctSubject.Rows.Count; k++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataRow dr = dtFinal.NewRow(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dr["Subject"] = distinctSubject.Rows[k][0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtFinal.Rows.Add(dr); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dsAdmissionNo.Tables.Add(dtFinal); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; dsAdmissionNo.Tables.Count; i++) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dataTable = dsAdmissionNo.Tables[i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string admissionNo = dsAdmissionNo.Tables[i].TableName.Split('_')[1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int row = 0; row &lt; dataTable.Rows.Count; row++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int column = 1; column &lt; dataTable.Columns.Count; column++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dataTable.Rows[row][column] = dt.Select("Subject='" + dataTable.Rows[row][0] + "' AND Exam='" + dataTable.Columns[column].ColumnName + "' AND AdmissionNo='" + admissionNo + "'")[0].ItemArray[6]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; for (int k = 0; k &lt; dsAdmissionNo.Tables.Count; k++) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PdfPTable pdfTable = new PdfPTable(dsAdmissionNo.Tables[0].Columns.Count); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable admissionNoTable = dsAdmissionNo.Tables[k]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 0; l &lt; admissionNoTable.Columns.Count; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase(admissionNoTable.Columns[l].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase(" ", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 1; l &lt; admissionNoTable.Columns.Count; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string exam = admissionNoTable.Columns[l].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string admission = admissionNoTable.TableName.Split('_')[1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase(max.ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int j = 0; j &lt; admissionNoTable.Rows.Count; j++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 0; l &lt; admissionNoTable.Columns.Count; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (l == 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase((j + 1) + "-" + admissionNoTable.Rows[j][l].ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase(admissionNoTable.Rows[j][l].ToString(), FontFactory.GetFont(FontFactory.HELVETICA, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase("Obtain", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 1; l &lt; admissionNoTable.Columns.Count; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int sum = Convert.ToInt32(admissionNoTable.Compute("SUM([" + admissionNoTable.Columns[l].ColumnName + "])", string.Empty)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase(sum.ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase("Max", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 1; l &lt; admissionNoTable.Columns.Count; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string exam = admissionNoTable.Columns[l].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string admission = admissionNoTable.TableName.Split('_')[1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase((max * admissionNoTable.Rows.Count).ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase("%", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 1; l &lt; admissionNoTable.Columns.Count; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string exam = admissionNoTable.Columns[l].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string admission = admissionNoTable.TableName.Split('_')[1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max = max * admissionNoTable.Rows.Count; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int obtained = Convert.ToInt32(admissionNoTable.Compute("SUM([" + admissionNoTable.Columns[l].ColumnName + "])", string.Empty)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase((obtained * 100 / max).ToString(), FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase("Grade", FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int l = 1; l &lt; admissionNoTable.Columns.Count ; l++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string grade = ""; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string exam = admissionNoTable.Columns[l].ToString(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string admission = admissionNoTable.TableName.Split('_')[1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int max = Convert.ToInt32(dt.Select("Exam='" + exam + "' AND AdmissionNo='" + admission + "'")[0].ItemArray[5]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max = max * admissionNoTable.Rows.Count; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int obtained = Convert.ToInt32(admissionNoTable.Compute("SUM([" + admissionNoTable.Columns[l].ColumnName + "])", string.Empty)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int percentage = obtained * 100 / max; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (percentage &gt;= 90) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grade = "O"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (percentage &gt;= 80 &amp;&amp; percentage &lt;= 89) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grade = "A+"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (percentage &gt;= 70 &amp;&amp; percentage &lt;= 79) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grade = "A"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (percentage &gt;= 50 &amp;&amp; percentage &lt;= 69) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grade = "B"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grade = "F"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pdfTable.AddCell(new Phrase(grade, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; doc.Add(pdfTable); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; doc.NewPage(); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; doc.Close(); }</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/5AWI61o.gif" alt="" width="544" height="259" /></p>https://www.aspforums.net:443/Threads/551991/Calculate-Sum-and-Percentage-for-each-column-and-Export-to-PDF-in-C-and-VBNet/https://www.aspforums.net:443/Threads/551991/Calculate-Sum-and-Percentage-for-each-column-and-Export-to-PDF-in-C-and-VBNet/Fri, 17 May 2019 04:43:24 GMT