|
-
Mar 15th, 2008, 08:27 AM
#1
Thread Starter
Hyperactive Member
[2008] First row missing when importing excel data to dataset
Hi all,
I have a small app that imports data from excel to a dataset. Then using the data for a DGV.
However, the first row does not go in to the dataset.
Code:
Private Sub ImporteraExcelfilToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImporteraExcelfilToolStripMenuItem.Click
Me.OpenFileDialog1.ShowDialog()
Dim myfile As String
myfile = Me.OpenFileDialog1.FileName.ToString
Dim cn As System.Data.OleDb.OleDbConnection
Dim cmda As System.Data.OleDb.OleDbDataAdapter
Dim ds As New System.Data.DataSet()
Dim irc, icc, idr As Integer
Dim sdc As New System.Data.DataColumn
On Error Resume Next
cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & _
"data source=" & myfile & ";Extended Properties=Excel 8.0;")
cmda = New System.Data.OleDb.OleDbDataAdapter("select * from [Blad1$]", cn)
cn.Open()
cmda.Fill(ds)
cn.Close()
irc = 0
icc = 0
If ds.Tables(0).Columns.Count > Me.DataGridView1.Columns.Count Then
icc = ds.Tables(0).Columns.Count - Me.DataGridView1.Columns.Count
Do While icc > 0
Me.DataGridView1.Columns.Add(Me.DataGridView1.Columns.Count + 1, Me.DataGridView1.Columns.Count + 1)
icc = ds.Tables(0).Columns.Count - Me.DataGridView1.Columns.Count
Loop
Else
For Each sdc In ds.Tables(0).Columns
Me.DataGridView1.Columns.Add(icc, icc)
icc = icc + 1
Next
End If
For Each item In ds.Tables(0).Rows
icc = 0
Me.DataGridView1.Rows.Insert(irc)
For Each sdc In ds.Tables(0).Columns
Me.DataGridView1.Rows(irc).Cells(icc).Value = ds.Tables(0).Rows(irc).Item(icc).ToString
icc = icc + 1
Next
irc = irc + 1
Next
End Sub
Please tell me what I´m doing wrong
Fuga.
Visual Studio 2010 xpress, Visual Studio 2008 pro, SQL Server, SQL Server management studio 2008 r2, MSAccess
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|