Establish that all the fields that they fill in the grid are string C # + Windows Forms

1

I have a problem filling a datagridview and it is that when filling it some data disappears in some cells, and I think it is because they are established with a predetermined format by the same datagrid when filled, I would like to know how to make all the data bring as a string or select a format by columns.

I add Codigo (I collect the data that there is in an excel and I add them in a datagrid)

//Aqui pregunto por la direccion donde esta el excel

DialogResult ListaUbiExcel = buscarlist.ShowDialog();

//Con esto valido la direccion y continuo con la sentencia

//dtgrilla es el nombre del DataGridView

if (ListaUbiExcel == DialogResult.OK)

               {
                    buscarlist.Title = "Selecciona el archivo excel (xls, xlsx)";
                    buscarlist.Filter = "*.xls|*.xlsx|All files (*.*)|*.*";
                    dtgrilla.Enabled = false;
                    string Direccion = buscarlist.FileName;
                    String connString = "Provider=Microsoft.Jet.OLeDb.4.0;Data Source= " + Direccion + ";Extended Properties= \"Excel 8.0;HDR = YES\"";  //Se crea el texto para la conexíón
                    OleDbConnection objConn = new OleDbConnection(connString);
                    objConn.Open();
                    DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    OleDbCommand consultar = default(OleDbCommand);
                    consultar = new OleDbCommand("Select * from [Hoja1$]", objConn);
                    OleDbDataAdapter lista = new OleDbDataAdapter();
                    lista.SelectCommand = consultar;
                    DataSet ds = new DataSet();
                    lista.Fill(ds);
                    dtgrilla.DataSource = ds.Tables[0];
                    objConn.Close();
    
asked by Andres M 02.10.2018 в 18:22
source

2 answers

0

I already managed to solve it, even make the code even easier, use Linq To Excel, to connect with excel directly, the library is achieved by installing it with the NuGet Manager of Visual Studio:

  

Class: Entity Excel Sheet

class EntidadHojaExcel
{
   public string Pagina { get; set; }
   public string Material { get; set; }
   public string Descripcion { get; set; }
   public string Linea_Grabacion { get; set; }
   public string Catalogo { get; set; }
   public string Color { get; set; }
   public string Talla { get; set; }
}
  

Class: List Excel

            //BuscarList Es un objeto OpenFileDialog(Me sirve para buscar un archivo en el equipo)

            buscarlist.Title = "Selecciona el archivo excel (xls, xlsx)";
            buscarlist.Filter = "*.xls|*.xlsx|All files (*.*)|*.*";
            DialogResult ListaUbiExcel = buscarlist.ShowDialog();
            string Direccion = buscarlist.FileName;

            //Entra si el usuario selecciona un archivo

            if (ListaUbiExcel == DialogResult.OK)
            {  

            var book = new ExcelQueryFactory(Direccion);
                            var resultado = (from row in book.Worksheet("Hoja1")
                             let item = new EntidadHojaExcel
                             {
                                 Pagina = row[0].Cast<string>(),
                                 Material = row[1].Cast<string>(),
                                 Descripcion = row[2].Cast<string>(),
                                 Linea_Grabacion = row[3].Cast<string>(),
                                 Catalogo = row[4].Cast<string>(),
                                 Color = row[5].Cast<string>(),
                                 Talla = row[6].Cast<string>()
                             }
                             select item).ToList();
            book.Dispose();
            MessageBox.Show("Cargado con Exito", "El documento se ha cargado", MessageBoxButtons.OK);

            //DataGrilla es el nombre del datagrid                

            dtgrilla.DataSource = resultado;
            dtgrilla.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
    
answered by 03.10.2018 / 17:51
source
2

The DataGridView by default fills all the columns that your DataSource brings. To avoid this, modify the AutoGenerateColumns property to false. Just remember that when this property is false, you must indicate to the DataGridView which columns you are going to show and the type of data, this can be done in design or in code.

    
answered by 02.10.2018 в 18:49