我是VB新手。单击按钮时,将Excel数据导入DataGridView。但整个表单和DataGridView变小了。为什么?
按钮点击功能:
Private Sub btImport_Click(sender As Object, e As EventArgs) Handles btImport.Click
txbStatus.Text = "Import data from excel..."
'DataGridView1.DataSource = GetTable()
DataGridView1.DataSource = excel_accerror.GetTable("C:\proj\test3.xlsx")
'DataGridView1.DataSource = excel_accerror.GetTable2()
End Sub
'从excel导入数据
Public Function GetTable(ByVal filepath As String) As System.Data.DataTable
Dim dt As New System.Data.DataTable
Using MyConn As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\pro\test3.xlsx';Extended Properties=Excel 12.0 Xml;") 'remove Xml, still works
Try
MyConn.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConn)
da.Fill(dt)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
Return dt
End Function
设置DataGridView,以及整个表单:
Me.txbStatus = New System.Windows.Forms.TextBox()
Me.DataGridView1 = New System.Windows.Forms.DataGridView()
Me.btExport = New System.Windows.Forms.Button()
Me.btImport = New System.Windows.Forms.Button()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'txbStatus
'
Me.txbStatus.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!)
Me.txbStatus.Location = New System.Drawing.Point(14, 430)
Me.txbStatus.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.txbStatus.Name = "txbStatus"
Me.txbStatus.Size = New System.Drawing.Size(488, 35)
Me.txbStatus.TabIndex = 1
'
'DataGridView1
'
Me.DataGridView1.AllowDrop = True
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.AllowUserToOrderColumns = True
DataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black
DataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.[False]
Me.DataGridView1.AlternatingRowsDefaultCellStyle = DataGridViewCellStyle2
Me.DataGridView1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.DataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.DataGridView1.GridColor = System.Drawing.SystemColors.Highlight
Me.DataGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl
Me.DataGridView1.Location = New System.Drawing.Point(527, 31)
Me.DataGridView1.Margin = New System.Windows.Forms.Padding(0)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.DataGridView1.RowHeadersWidth = 31
Me.DataGridView1.RowTemplate.Height = 28
Me.DataGridView1.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
Me.DataGridView1.Size = New System.Drawing.Size(491, 519)
Me.DataGridView1.TabIndex = 12
'
'btExport
'
Me.btExport.Location = New System.Drawing.Point(14, 516)
Me.btExport.Name = "btExport"
Me.btExport.Size = New System.Drawing.Size(75, 53)
Me.btExport.TabIndex = 13
Me.btExport.Text = "Export"
Me.btExport.UseVisualStyleBackColor = True
'
'btImport
'
Me.btImport.Location = New System.Drawing.Point(95, 516)
Me.btImport.Name = "btImport"
Me.btImport.Size = New System.Drawing.Size(75, 53)
Me.btImport.TabIndex = 14
Me.btImport.Text = "Import"
Me.btImport.UseVisualStyleBackColor = True
'
'frmDemo
'
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None
Me.ClientSize = New System.Drawing.Size(1052, 580)
Me.Controls.Add(Me.btImport)
Me.Controls.Add(Me.btExport)
Me.Controls.Add(Me.DataGridView1)
Me.Controls.Add(Me.txbStatus)
Me.DoubleBuffered = True
Me.Margin = New System.Windows.Forms.Padding(3, 4, 3, 4)
Me.Name = "frmDemo"
Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show
Me.Text = "DEMO"
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
答案 0 :(得分:0)
也许这是System.Data.OleDb中的一个错误。
Using MyConn As New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\susu\usbproj\test3.xlsx';Extended Properties=Excel 12.0 Xml;") 'remove Xml, still works
Try
MyConn.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConn)
da.Fill(dt)
MyConn.Close()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
卸载我的第一个Microsoft Access数据库引擎2010。 然后在这里安装第一个https://www.microsoft.com/en-ca/download/details.aspx?id=13255
感谢此处发布类似但不同的帖子:OleDb / OleDbConnection resizing form (bug?)