Usuário com melhor resposta
id datagrid

Pergunta
-
Eu deixei o campo id invisível na datagrid, mais ele esta saindo na exportação do meu pdf. O que fazer para isso não acontecer?
PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount); pdfTable.DefaultCell.Padding = 2; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; pr5dk foreach (DataGridViewColumn column in dataGridView1.Columns) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240); pdfTable.AddCell(cell); } foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value != null) { if (cell.Value is DateTime) { DateTime data; DateTime.TryParse(Convert.ToString(cell.Value), out data); pdfTable.AddCell(data.ToShortDateString()); } else { pdfTable.AddCell(cell.Value.ToString()); } } } } string folderPath = "C:\\PDFs\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } string FileName = folderPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; using (FileStream stream = new FileStream(FileName, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } SendFileToPrinter(@"C:\\PDFs\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"); }
Respostas
-
Ok, tenta dessa forma:
PdfPTable pdfTable = new PdfPTable(dataGridView1.DisplayedColumnCount(true)); pdfTable.DefaultCell.Padding = 2; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; pr5dk foreach (DataGridViewColumn column in dataGridView1.Columns) { if (column.Visible) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240); pdfTable.AddCell(cell); } } foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Visible) { if (cell.Value != null) { if (cell.Value is DateTime) { DateTime data; DateTime.TryParse(Convert.ToString(cell.Value), out data); pdfTable.AddCell(data.ToShortDateString()); } else { pdfTable.AddCell(cell.Value.ToString()); } } } } } string folderPath = "C:\\PDFs\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } string FileName = folderPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; using (FileStream stream = new FileStream(FileName, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } SendFileToPrinter(@"C:\\PDFs\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"); }
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco- Editado André SeccoMVP quinta-feira, 15 de setembro de 2016 13:30
- Marcado como Resposta SouthNew quinta-feira, 15 de setembro de 2016 13:32
Todas as Respostas
-
Olá,
Você pode colocar uma verificação: "Se a coluna não estiver Visível" então não adiciona.
Desta maneira:
PdfPTable pdfTable = new PdfPTable(dataGridView1.DisplayedColumnCount(true)); pdfTable.DefaultCell.Padding = 2; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; pr5dk foreach (DataGridViewColumn column in dataGridView1.Columns) { if (column.Visible) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240); pdfTable.AddCell(cell); } } foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value != null) { if (cell.Value is DateTime) { DateTime data; DateTime.TryParse(Convert.ToString(cell.Value), out data); pdfTable.AddCell(data.ToShortDateString()); } else { pdfTable.AddCell(cell.Value.ToString()); } } } } string folderPath = "C:\\PDFs\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } string FileName = folderPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; using (FileStream stream = new FileStream(FileName, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } SendFileToPrinter(@"C:\\PDFs\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"); }
Valeu!
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco
- Editado André SeccoMVP quinta-feira, 15 de setembro de 2016 12:19
-
-
Ok, tenta dessa forma:
PdfPTable pdfTable = new PdfPTable(dataGridView1.DisplayedColumnCount(true)); pdfTable.DefaultCell.Padding = 2; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; pdfTable.DefaultCell.BorderWidth = 1; pr5dk foreach (DataGridViewColumn column in dataGridView1.Columns) { if (column.Visible) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240); pdfTable.AddCell(cell); } } foreach (DataGridViewRow row in dataGridView1.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Visible) { if (cell.Value != null) { if (cell.Value is DateTime) { DateTime data; DateTime.TryParse(Convert.ToString(cell.Value), out data); pdfTable.AddCell(data.ToShortDateString()); } else { pdfTable.AddCell(cell.Value.ToString()); } } } } } string folderPath = "C:\\PDFs\\"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } string FileName = folderPath + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; using (FileStream stream = new FileStream(FileName, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } SendFileToPrinter(@"C:\\PDFs\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"); }
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco- Editado André SeccoMVP quinta-feira, 15 de setembro de 2016 13:30
- Marcado como Resposta SouthNew quinta-feira, 15 de setembro de 2016 13:32
-
-
Realmente, escrevi errado. Agora consertei na resposta anterior. Tenta novamente.
Se a resposta for relevante ou tenha resolvido seu problema, marque como útil/resposta!
André Secco
Microsoft MSP & MSDN Tech Advisor
Blog: http://andresecco.com.br
GitHub: http://github.com/andreluizsecco
Twitter: @andre_secco