Hello! my question is: can an excel table be displayed in a windows forms datagridview with the DbDataAdapter.Fill method?
below I attach the code that I achieve, the problem is that when executing the script revit and dynamo are closed
I also attach the source from where I interpret the code
code:
import clr
#add a reference
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
clr.AddReference("Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
clr.AddReference("System.Data") #para datagridview
#import library
import System.Drawing
import System.Windows.Forms
import Microsoft.Office.Interop.Excel as Excel
import System.Data.OleDb #para datagridview
import System.Data #para datagridview
from System.Windows.Forms import *
from System.Drawing import *
from Microsoft.Office.Interop import Excel
from System.Runtime.InteropServices import Marshal
from System.Data import * #para datagridview
from System.Data.OleDb import * #para datagridview
aux = [ ]
#COLORES DE BOTONES
PLB1Color = Color.FromArgb(4,41,68)
PLB1ClikedColor = Color.FromArgb(32,58,85)
#COLORES TEXTO BOTONES
BFOscuroColor = Color.LightGray
BFClaroColor = Color.DarkGray
#ventana windows forms
class WindowForm(Form):
--def __init__(self):
-------self.Show()
-------self.Size = Size(400,480)
#Label descriptiva
-------self.label1 = Label()
-------self.label1.Text = 'LISTADO DE PROYECTOS:'
-------self.label1.Size = Size(300,30)
-------self.label1.ForeColor = BFClaroColor
-------self.label1.Location = Point(20,20)
#espacio donde aparecera el excel
-------self.DataGridView1 = DataGridView()
-------self.DataGridView1.Size = Size(350,300)
-------self.DataGridView1.Location = Point(20,60)
-------self.DataGridView1.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom)
#boton para importar el excel
-------self.Button1 = Button()
-------self.Button1.Text = 'IMPORTAR'
-------self.Button1.Size = Size(100,30)
-------self.Button1.ForeColor = BFOscuroColor
-------self.Button1.BackColor = PLB1ClikedColor
-------self.Button1.FlatStyle = FlatStyle.Flat
-------self.Button1.FlatAppearance.BorderSize = 0
-------self.Button1.TextAlign = ContentAlignment.MiddleLeft
-------self.Button1.Location = Point(200,380)
-------self.Button1.Padding = System.Windows.Forms.Padding(10,0,0,0)
-------self.Button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right)
-------self.Button1.Click += self.Button1Clicked
#los elementos que controla la ventana windows forms
-------self.Controls.Add(self.label1)
-------self.Controls.Add(self.DataGridView1)
-------self.Controls.Add(self.Button1)
#funcion del Boton Importar
--def Button1Clicked(self, sender, args):
#SELECCIONAR EL ARCHIVO .xlsx
-------openFileDialog = OpenFileDialog()
-------openFileDialog.Filter = "Excel |*.xls;*.xlsx;"
-------openFileDialog.Title = "Seleccionar Archivo"
#ABRIR EXCEL EN BACKGROUND E IMPROTAR LA Hoja1
-------if openFileDialog.ShowDialog() == DialogResult.OK:
--------------filename = str(openFileDialog.FileName)
#cadena de conexion
--------------connectionString = 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' + filename + ';Extended Properties="Excel 12.0 Xml;HDR=YES";'
--------------conn = OleDbConnection(connectionString)
--------------ds = DataSet()
--------------dt = DataTable()
#da = OleDbDataAdapter()
--------------ds.Tables.Add(dt)
--------------#consulta = "Select * from [Hoja1$]"
--------------#da = OleDb.OleDbDataAdapter(consulta,conn)
--------------da = OleDbDataAdapter(OleDbCommand("select * from [Hoja1$]", conn))
--------------da.Fill(ds)
--------------self.DataGridView1.DataSource = dt
--------------#aux.append(str(ds))
form = WindowForm()
Application.Run(form)
#OUT = aux
source:
the problem arises when performing the fill method