elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
28 Mayo 2012, 05:20  


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)

+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (Moderador: [D4N93R])
| | | |-+  Exportar un Datagridview a PDF con un encabezado personalizado
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Exportar un Datagridview a PDF con un encabezado personalizado  (Leído 960 veces)
joalbela

Desconectado Desconectado

Mensajes: 30



Ver Perfil
Exportar un Datagridview a PDF con un encabezado personalizado
« en: 11 Marzo 2011, 02:49 »

Hola amigos tengo este codigo que genera un reporte en un datagridview y luego lo exporta a PDF, me gustaria que el encabezado donde dice los nombres de las columnas tuviera una letra en negrilla y que esta fila fuera un poco mas gruesa y con un color de fondo pero no se como hacerle les agredeceria mucho su ayuda.




Código
            IfxDataAdapter dt = new IfxDataAdapter(" SELECT a.cdgo_prdcto as CODIGO,a.nmbre_prdcto DESCRIPCION, SUM(b.cntdad)PMDIOVTA ,a.csto_rpscion CR, " +
                                                  " a.clfccion_prmnnte P, a.clfccion_tmpral T,a.cdgo_brra EAN" +
                                                  " from prdcto a , prmdio_vnta b " +
                                                  " where estdo = 'A' " +
                                                  " and a.cdgo_prdcto = b.cdgo_prdcto " +
                                                  " group by 1,2,4,5,6,7 " +                                                                                                                            
                                                   " order by 2 ", conexion);
           DataGridTableStyle ts1 = new DataGridTableStyle();
 
 
 
            DataSet dsdatos = new DataSet();
            dt.Fill(dsdatos, "Datos");                        
 
           this.dataGridView1.DataMember = "Datos";
           //this.dataGridView1.Columns(1).Width = 4400;
           this.dataGridView1.DataSource = dsdatos;
 
 
 
 
       }
 
       private void Form1_Load(object sender, EventArgs e)
       {
 
       }
 
       private void btnExportar_Click(object sender, EventArgs e)
       {
 
 
           DateTime hora = DateTime.Now;
           string fcha_ttal = Convert.ToDateTime(hora).Day + "/" + Convert.ToDateTime(hora).Month + "/" + Convert.ToDateTime(hora).Year;
 
 
           Document doc = new Document(PageSize.A4, 9, 9, 10, 10);
           string filename = "C:\\Temp\\Listado Total.pdf";
           iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(@"C:\Mis documentos\Biblia_productos\Biblia_productos\Images\marion.JPG"); jpg.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
           Chunk encab = new Chunk(" LISTA DE PRODUCTOS CODIFICADOS EN " + fcha_ttal + "", FontFactory.GetFont("COURIER", 12));
 
 
           try
           {
               FileStream file = new FileStream
              (filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
               PdfWriter.GetInstance(doc, file);
               doc.Open();
 
 
               doc.Add(new Paragraph(encab));
               doc.Add(jpg);
               GenerarDocumento(doc);
 
               Process.Start(filename);
               doc.Close();
           }
 
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
 
       }
 
       //Función que genera el documento Pdf
       public void GenerarDocumento(Document document)
       {
           PdfPTable datatable = new PdfPTable(dataGridView1.ColumnCount);
           datatable.DefaultCell.Padding = 1;
           float[] headerwidths = GetTamañoColumnas(dataGridView1);
 
           datatable.SetWidths(headerwidths);
           datatable.WidthPercentage = 100;
           datatable.DefaultCell.BorderWidth = 2;
 
           iTextSharp.text.Font fuente = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER);
 
           DataGridViewCellStyle style = this.dataGridView1.ColumnHeadersDefaultCellStyle;
 
           Phrase objP = new Phrase("A", fuente);
 
           datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
           for (int i = 0; i < dataGridView1.ColumnCount; i++)
           {
 
               objP = new Phrase(dataGridView1.Columns[i].HeaderText, fuente);
                datatable.HorizontalAlignment = Element.ALIGN_CENTER;
 
               datatable.AddCell(objP);
 
 
           }
           datatable.HeaderRows =1;
 
           datatable.DefaultCell.BorderWidth = 1;
 
 
 
 
           for (int i = 0; i < dataGridView1.RowCount - 1; i++)
           {
               for (int j = 0; j < dataGridView1.ColumnCount; j++)
               {
                   objP = new Phrase(dataGridView1[j, i].Value.ToString(), fuente);
 
                   style.BackColor = Color.WhiteSmoke;
                   style.ForeColor = Color.Gray;
 
                   datatable.AddCell(objP);
 
 
 
               }
               datatable.CompleteRow();
           }
           document.Add(datatable);
       }
       public float[] GetTamañoColumnas(DataGridView dg)
       {
           float[] values = new float[dg.ColumnCount];
           for (int i = 0; i < dg.ColumnCount; i++)
           {
               values[i] = (float)dg.Columns[i].Width;
           }
           return values;
       }


En línea

Jorge MB
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Archivos de Encabezado de C++
Programación General
El Viejo del Baston 0 368 Último mensaje 8 Marzo 2004, 23:11
por El Viejo del Baston
Tipos de encabezado
Desarrollo Web
kosis157 1 4,027 Último mensaje 28 Julio 2004, 09:22
por el-brujo
Sniffer y encabezado HTTP
Nivel Web
Achernar 0 1,585 Último mensaje 19 Octubre 2007, 14:34
por Achernar
Exportar DataGridView a Excel (CODIGO)
.NET
elmaro 5 14,008 Último mensaje 28 Septiembre 2011, 23:49
por LROJASAYLLON
[Aporte] Exportar DataGridView a Excel, PDF y HTML « 1 2 »
.NET
oscarj24 28 12,784 Último mensaje 27 Abril 2012, 03:50
por William1995
Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines