Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27061

VS 2012 Problem importing csv file into datagridview from selected line in textbox preview

$
0
0
I am receiving several (50+) CSV Files that will need to be added to tables in our database. I am trying to generate script to speed up this process by reading the CSV File (; delimited) into a textbox preview of the first 10 lines. Then I can select the line containing the headers by entering the line number and hitting a button to extract the header row. Initially I did this by pulling the field lables in to a textbox, but I am going to need to add data types and min/max lengths, etc. So, I want to pull the entire file into a datagridview as well, but I want to start the datarows at the line number entered in the previous step.

Can anyone help me out?

Here is a picture of the Form1 of this project.
Name:  Form1withData.jpg
Views: 12
Size:  141.4 KB

Code:


'My Code Here
Imports System.IO

Public Class Form1
    Dim mData() As String

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        ofdSelectCSV.ShowDialog()
    End Sub

    Private Sub ofdSelectCSV_Fileok(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ofdSelectCSV.FileOk
        Dim i As Integer

        mData = File.ReadAllLines(ofdSelectCSV.FileName)
        txtPreview.Text = ""
        For i = 0 To 9
            txtPreview.AppendText(i + 1 & ":" & vbTab & mData(i) & vbCrLf)
        Next
    End Sub

Now, I can get the field names into a text box with this:

Code:

'My Code Here
  Private Sub btnExtractFieldNames_Click(sender As Object, e As EventArgs) Handles btnExtractFieldNames.Click
        txtFieldNames.Text = mData(Val(txtRowNum.Text) - 1).Replace(";", vbCrLf).Replace(Chr(24), "")
        End Sub
End Class


But I want to import the whole delimited csv file into a datagridview instead, beginning on the line selected by the user above.
I have tried several things, but this is what I still have in my code right now and it doesn't work anyway. I'm looking for help to code the last step.

Code:

'My Code Here
    Private Sub btnExtractFieldNames_Click(sender As Object, e As EventArgs) Handles btnExtractFieldNames.Click
        Dim SelectedRows As String
        SelectedRows = mData(Val(txtRowNum.Text) - 1).Replace(";", vbTab)
    End Sub
End Class


Here is a sample CSV File:

"SJD_TEST SALES CREATE DATE"
"Latest Data Update:"
"28.06.2013 10:40:27"

"SalesOrderNo";"DocumentNo";"ItemNo";"PriorityCd";"Category";"AnalystCd";"Plant";"DayCreated";"Rejec tionReason";"RejectionStatus";"SupplyStatus";"DeliveryBlk";"QTY_UI"
"1234567890";"A12BC33211D456";"10";"13";"ZA0A";"A1B01";"5555";"27.06.2013";"#";"A";"BD";"#";"10 EA"
"1234567891";"A12BC33211D457";"10";"12";"ZA0A";"A1B02";"5402";"27.06.2013";"#";"A";"BA";"#";"55, 000 EA"
"1234567892";"A12BC33211D458";"10";"12";"ZA0A";"A1B03";"5912";"24.06.2013";"#";"A";"BA";"#";"120 EA"
"1234567893";"A12BC33211D459";"10";"12";"ZA0A";"A1B04";"5912";"24.06.2013";"#";"A";"BA";"#";"120 EA"
"1234567894";"A12BC33211D460";"10";"6";"ZA0A";"A1B05";"5932";"25.06.2013";"#";"A";"BA";"#";"480, 000 EA"
"1234567895";"A12BC33211D461";"10";"12";"ZA0A";"A1B06";"5912";"24.06.2013";"#";"A";"BA";"#";"3,000.0 00 FT"
"1234567896";"A12BC33211D462";"10";"13";"ZA0A";"A1B07";"5932";"24.06.2013";"#";"A";"BA";"#";"4,3 20 EA"
"1234567897";"A12BC33211D463";"10";"12";"ZA0A";"A1B08";"5912";"24.06.2013";"#";"A";"BA";"#";"40 EA"
"1234567898";"A12BC33211D464";"10";"9";"ZA0A";"A1B09";"5960";"25.06.2013";"#";"A";"BA";"#";"24 EA"
"1234567899";"A12BC33211D465";"10";"12";"ZA0A";"A1B10";"5922";"24.06.2013";"#";"A";"BA";"#";"4,000.0 00 FT"
"1234567900";"A12BC33211D466";"10";"9";"ZA0A";"A1B11";"5555";"27.06.2013";"#";"A";"BD";"#";"3,00 0 EA"
"1234567901";"A12BC33211D467";"10";"12";"ZA0A";"A1B12";"5932";"24.06.2013";"#";"A";"BA";"#";"100 EA"
"1234567902";"A12BC33211D468";"10";"6";"ZA0A";"A1B13";"5922";"25.06.2013";"#";"A";"BA";"#";"40,8 00 EA"
"1234567903";"A12BC33211D469";"10";"12";"ZA0A";"A1B14";"5912";"24.06.2013";"#";"A";"BA";"#";"60, 000 EA"
"1234567904";"A12BC33211D470";"10";"13";"ZA0A";"A1B15";"5922";"24.06.2013";"#";"A";"BA";"#";"144 EA"
"1234567905";"A12BC33211D471";"10";"13";"ZA0A";"A1B16";"5922";"24.06.2013";"#";"A";"BA";"#";"1,8 00 EA"
"1234567906";"A12BC33211D472";"10";"13";"ZA0A";"A1B17";"5922";"24.06.2013";"#";"A";"BA";"#";"1,2 00 EA"
"1234567907";"A12BC33211D473";"10";"13";"ZA0A";"A1B18";"5922";"24.06.2013";"#";"A";"BA";"#";"100 EA
"1234567908";"A12BC33211D474";"10";"13";"ZA0A";"A1B19";"5922";"24.06.2013";"#";"A";"BA";"#";"1,4 40 EA"
"1234567909";"A12BC33211D475";"10";"13";"ZA0A";"A1B20";"5922";"24.06.2013";"#";"A";"BA";"#";"32, 000 EA"
"1234567910";"A12BC33211D476";"10";"13";"ZA0A";"A1B21";"5922";"24.06.2013";"#";"A";"BA";"#";"1 KT"
"1234567911";"A12BC33211D477";"10";"13";"ZA0A";"A1B22";"5922";"24.06.2013";"#";"A";"BA";"#";"25, 200 EA"
"1234567912";"A12BC33211D478";"10";"13";"ZA0A";"A1B23";"5922";"24.06.2013";"#";"A";"BA";"#";"2,2 80 EA"
"1234567913";"A12BC33211D479";"10";"13";"ZA0A";"A1B24";"5922";"24.06.2013";"#";"A";"BA";"#";"13, 440 EA"
"1234567914";"A12BC33211D480";"10";"13";"ZA0A";"A1B25";"5922";"24.06.2013";"#";"A";"BA";"#";"11, 000 EA"
"1234567915";"A12BC33211D481";"10";"13";"ZA0A";"A1B26";"5922";"24.06.2013";"#";"A";"BA";"#";"100 EA"
"1234567916";"A12BC33211D482";"10";"13";"ZA0A";"A1B27";"5922";"24.06.2013";"#";"A";"BA";"#";"48 EA"
"1234567917";"A12BC33211D483";"10";"13";"ZA0A";"A1B28";"5922";"24.06.2013";"#";"A";"BA";"#";"40 EA"
"1234567918";"A12BC33211D484";"10";"13";"ZA0A";"A1B29";"5922";"24.06.2013";"#";"A";"BA";"#";"483 ,840 EA"

Any help would be greatly appreciated.

Thanks! Shannon
Attached Images
  

Viewing all articles
Browse latest Browse all 27061

Trending Articles