problem when importing from excel file

  • Thread starter Thread starter Houssem12
  • Start date Start date
H

Houssem12

Guest
Hi members

when i import an excel fileto a datatable the first rows it be the column name in the datatgridview

so i created another datatable where i named the column

after that the code work perfectly but the first row become unreadeble it goes directly to 2nd row

this my code

openFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
openFileDialog.Filter = "All File (*.*)|*.*|Excel Files(*.xlsx)|*xlsx|Xls Files(*.xls)|*.xls"
If openFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Dim fi As New IO.FileInfo(openFileDialog.FileName)
Dim FileName As String = openFileDialog.FileName
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + excel + ";Extended Properties=Excel 12.0;")
Dim command As New OleDbCommand("select * from [Feuil1$]", conn)
Dim dta2 As New OleDbDataAdapter(command)

dta2.Fill(tab2)
conn.Close()


DT.Columns.Add("Command", GetType(String)) 'txt column
DT.Columns.Add("Article", GetType(String)) 'txt column
DT.Columns.Add("Désignation", GetType(String)) 'txt column
DT.Columns.Add("Qte", GetType(String)) 'sql column
DT.Columns.Add("PU", GetType(Double)) 'txt column

For n = 0 To tab2.Rows.Count - 1
Dim Nrow As DataRow = DT.NewRow
Nrow("Command") = tab2.Rows(n).Item(0)
Nrow("Article") = tab2.Rows(n).Item(1)
Nrow("Qte") = tab2.Rows(n).Item(2)
Nrow("PU") = tab2.Rows(n).Item(3)
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn.Open()
Dim cmd23 As New SqlCommand("select AR_Design from F_ARTICLE where ar_ref='" & Nrow("Article").ToString & "'", cn)
Dim lbn As SqlDataReader = (cmd23.ExecuteReader)
lbn.Read()


Nrow("Désignation") = lbn(0)

DT.Rows.Add(Nrow)
Next
For i = 1 To DT.Rows.Count - 1
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn.Open()
Dim cmd5 As New SqlCommand("select count(AR_Ref) From F_article where ar_ref='" & DT.Rows(i).Item(1).ToString & "'", cn)

Dim ret = cmd5.ExecuteScalar
If ret = 0 Then
MessageBox.Show("Article n'existe pas : " & DT.Rows(i).Item(1).ToString & " BON COMMANDE : " & DT.Rows(i).Item(0).ToString)
Exit Sub
Else



End If

Next


DT.Columns("Désignation").SetOrdinal(2)
DataGridView4.RowHeadersWidth = 53

DataGridView4.DataSource = DT
End If


problem when i import from excel the first row becom the column name

even i did not write names of the columns in the excel file

so here the problem is the name of column and the 1st row in excel file

please how i manage to solve my problem

Continue reading...
 
Back
Top