Goran Bibic

Goran Bibic

  • 482
  • 2.9k
  • 198.2k

Itext pdf print after selection index changed in combobox?

Jul 3 2018 2:21 AM
Itext pdf print after selection index changed in combobox
 
After selection index changed have new values in datagrid print empty
 
Have header no have datagrid
 
Simle pdf document
  1. private void pdfButton_Click(object sender, EventArgs e)  
  2.         {  
  3.             //Creating iTextSharp Table from the DataTable data  
  4.             PdfPTable pdfTable = new PdfPTable(prijava_radnikaDataGridView.ColumnCount);  
  5.             pdfTable.DefaultCell.Padding = 3;  
  6.             pdfTable.WidthPercentage = 100;  
  7.             pdfTable.HorizontalAlignment = Element.ALIGN_CENTER;  
  8.             pdfTable.DefaultCell.BorderWidth = 1;  
  9.             float[] widths = new float[] { 18f, 40f, 35f, 35f};  
  10.             pdfTable.SetWidths(widths);  
  11.             BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
  12.             iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.WHITE);  
  13.             iTextSharp.text.Font calibri2 = new iTextSharp.text.Font(bfCalibri, 9);  
  14.   
  15.             //Adding Header row  
  16.             foreach (DataGridViewColumn column in prijava_radnikaDataGridView.Columns)  
  17.             {  
  18.   
  19.   
  20.                   
  21.                     PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri));  
  22.                     cell.BackgroundColor = new BaseColor(89, 135, 178);  
  23.                     cell.VerticalAlignment = Element.ALIGN_MIDDLE;  
  24.                     pdfTable.AddCell(cell);  
  25.                   
  26.             }  
  27.   
  28.             //Adding DataRow  
  29.             foreach (DataGridViewRow row in prijava_radnikaDataGridView.Rows)  
  30.             {  
  31.                 foreach (DataGridViewCell cell in row.Cells)  
  32.                 {  
  33.                      
  34.   
  35.                         pdfTable.AddCell(new PdfPCell(new Phrase(cell.Value.ToString(), calibri2)));  
  36.   
  37.                       
  38.                 }  
  39.             }  
  40.   
  41.   
  42.   
  43.             //Exporting to PDF  
  44.   
  45.             string folderPath = "C:\\PDFs\\";  
  46.             if (!Directory.Exists(folderPath))  
  47.             {  
  48.                 Directory.CreateDirectory(folderPath);  
  49.             }  
  50.             using (FileStream stream = new FileStream(folderPath + "Izvještaj nabavke po dobavljacima.pdf", FileMode.Create))  
  51.             {  
  52.                 iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 14);  
  53.                 iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 10);  
  54.                 iTextSharp.text.Font calibrinaslov = new iTextSharp.text.Font(bfCalibri, 16);  
  55.                 Document pdfDoc = new Document(PageSize.A4, 27f, 10f, 10f, 30f);  
  56.                 // PdfWriter.GetInstance(pdfDoc, stream);  
  57.                 PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);  
  58.                 pdfDoc.Open();   
  59.                 pdfDoc.Add(new ListItem(" "));  
  60.                 pdfDoc.Add(new ListItem(" "));  
  61.                 pdfDoc.Add(new ListItem(" Izvještaj nabavke po dobavljacima", calibrinaslov));  
  62.                 pdfDoc.Add(new ListItem(" "));  
  63.                 pdfDoc.Add(new ListItem(" "));  
  64.   
  65.   
  66.                  
  67.                 pdfDoc.Close();  
  68.                 stream.Close();  
  69.                 System.Diagnostics.Process.Start(folderPath + "Izvještaj nabavke po dobavljacima.pdf");  
  70.             }  
  71.         } 
Datagrid creted on this way
  1.  private void radni_sati_radnika_zbirno()  
  2.         {  
  3.             using (SqlConnection openCon = new SqlConnection(cs))  
  4.   
  5.             {  
  6.   
  7.  string saveStaff = " declare @filterDate date; SELECT  [radnik] as Radnik, "  
  8.                      // + "DATEADD(DAY, DATEDIFF(DAY, 0, vrijemeprijave), 0) AS [Datum] , "  
  9.                      + "SUM(cast((CASE WHEN DATEPART(WEEKDAY, vrijemeprijave) in (1, 7) then 0 else (CASE WHEN(DATEDIFF(SECOND, vrijemeprijave, vrijemeodjave) / 3600) > 8 THEN 8 ELSE(DATEDIFF(SECOND, vrijemeprijave, vrijemeodjave) / 3600) END) end)as float))   AS 'radni_sati',"  
  10.                      + "SUM(cast((CASE WHEN DATEPART(WEEKDAY, vrijemeodjave) in (1, 7) then(DATEDIFF(SECOND, vrijemeprijave, vrijemeodjave) / 3600) else(case when(DATEDIFF(SECOND, vrijemeprijave, vrijemeodjave) / 3600) > 8  then((DATEDIFF(SECOND, vrijemeprijave, vrijemeodjave) / 3600) - 8) ELSE 0 END) end)as float))   AS 'Prekovremeni'"  
  11.                      + "from [prijava_radnika]"  
  12.                      + "where MONTH([vrijemeprijave]) = '" + mjeseccomboBox.Text.ToString() + "'  "  
  13.                      + "group by [radnik]";  
  14.   
  15. openCon.Open();  
  16.                 using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))  
  17.   
  18.                 {  
  19.                     DataTable dt = new DataTable();  
  20.                     DataColumn column = new DataColumn();  
  21.                     column.DataType = System.Type.GetType("System.Int32");  
  22.                     column.ColumnName = "redni_broj";  
  23.                     column.AutoIncrement = true;  
  24.                     column.AutoIncrementSeed = 1;  
  25.                     column.AutoIncrementStep = 1;  
  26.                     dt.Columns.Add(column);  
  27.                     int index = -1;  
  28.                     foreach (DataRow row in dt.Rows)  
  29.                     {  
  30.                         row.SetField(column, ++index);  
  31.                     }  
  32.   
  33.                     querySaveStaff.Fill(dt);  
  34.                     prijava_radnikaDataGridView.DataSource = dt;  
  35.                     prijava_radnikaDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;  
  36.  prijava_radnikaDataGridView.Columns["redni_broj"].FillWeight = 150;  
  37.                     prijava_radnikaDataGridView.Columns["radnik"].FillWeight = 350;  //radnik  
  38.  prijava_radnikaDataGridView.Columns["radni_sati"].FillWeight = 150;                  //radni sati  
  39.                     prijava_radnikaDataGridView.Columns["Prekovremeni"].FillWeight = 150;                //prekovremeni  
  40.   
  41. prijava_radnikaDataGridView.Update();  
  42.                     prijava_radnikaDataGridView.Refresh();  
  43.                 }  
  44.   
  45.             }  
  46. }

Answers (1)