1

Tengo una duda.

Tengo este código para el llenado de un datagridview, el cual obtiene los datos de un documento excel.

string constr = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 12.0 Xml;");

OleDbConnection con = new OleDbConnection(constr);
//Hacer los comandos

OleDbCommand oconn = new OleDbCommand("Select * From [MPS 
Sheet$A16:AP]", con);


con.Open();

//Hacer los adapters
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);

//hacer los datatables
DataTable data = new DataTable();



ds.Tables.Add(data);
sda.Fill(data);
//Hacer las tablas
dataGridView1.DataSource = data;
//dataGridView7.DataSource = fecha;
con.Close();

Lo que quiero hacer ahora, es obtener solo tres columnas de las que tiene este datagridview, para colocarlos en otro nuevo datagridview.

Mi duda es ¿Se puede hacer? y si se puede ¿Como se haría?

Gracias

1 respuesta 1

1

Si, sí es posible. Solo hay que especificar las columnas que necesitas. Este es un ejemplo que me brindo un compañero, espero te sirva.

 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Data;   


 namespace BDPROG_CSharp
 {
     class classExcel
     {
         public int columnas;
         DataTable dt;
         Microsoft.Office.Interop.Excel.Application oXL;
         Microsoft.Office.Interop.Excel._Worksheet oSheet;
         Microsoft.Office.Interop.Excel.Range oRng;           

         public classExcel(int columnas, DataTable dt)
         {
             this.columnas = columnas;
             this.dt = new DataTable();
             this.dt = dt;
         }
         public void crearexcel()
         {

             oXL = new Microsoft.Office.Interop.Excel.Application();
             oXL.Visible = true;
             //Creamos un nuevo libro de Excel
             oXL.Workbooks.Add();
             oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oXL.ActiveSheet;
             //Le asignamos a la hoja 1 el nombre de "Datos"
             oSheet.Name = "Datos";
             //Pegamos el contenido
             oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, 1]).Select();
             oXL.ActiveCell.PasteSpecial();
             oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, 1]).EntireColumn.Delete();

             ////Creamos los encabezados                                     
             for (int z = 0; z <= columnas; z++)
             {
                 oSheet.Cells[1, z + 1] = Convert.ToString(dt.Columns[z].ColumnName);

                 //  oSheet.get_Range("A1", "D1").Font.Bold = true;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Font.ColorIndex = 2;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Interior.ColorIndex = 5;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Font.Bold = true;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).Font.Italic = true;
                 oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]).VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                 // oRng = (oSheet.Cells[1, z], oSheet.Cells[1, z]);
                 oRng = oSheet.get_Range(oSheet.Cells[1, z + 1], oSheet.Cells[1, z + 1]);
                 oRng.EntireColumn.AutoFit();

             }






         }
     }
 }
3
  • pero esto es para obtener el rango desde el documento excel ¿no?, lo que yo necesito es a partir del datagridview que ya llené con anterioridad,llenar otro datagridview con solo ciertas columnas del primero el 18 abr. 2017 a las 16:22
  • Si verificas a partir del for, solo especificas las columnas que quieres exportar.
    – Ric_hc
    el 18 abr. 2017 a las 17:35
  • Gracias. Ya me funcionó el 21 abr. 2017 a las 14:58

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.