从CSV导入行未正确导入

时间:2018-04-09 14:47:30

标签: .net vb.net

我希望你们周末愉快!

我目前正在尝试将CS​​V文件导入到dataset.tables(" CSVImportedFile")。这是我的代码:

    Dim ImportCSV As New OpenFileDialog
    With ImportCSV
        .Title = "Import CSV File"
        .Filter = "PowerBI (*.csv)|*.csv"
    End With

    If ImportCSV.ShowDialog = DialogResult.OK Then
        Dim CSVPath As String = ImportCSV.FileName
        Dim CSVDirectory As String = Path.GetDirectoryName(CSVPath) & "\"
        Dim CSVFile As String = Path.GetFileName(CSVPath)

        Using ImportConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & CSVDirectory & ";Extended Properties=""Text;HDR=YES;""")
            Using ImportAdapter As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & CSVFile & "]", ImportConnection)
                Try
                    ImportAdapter.Fill(dset.Tables("CSVImportedFile"))
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
                ImportConnection.Close()
            End Using
        End Using

        For Each CSVRows As DataRow In dset.Tables("CSVImportedFile").Rows
            'Trying to find the ID within the dataset column "TempID"
            Dim FindID As Integer = ourBindingSource.Find("TempID", CSVRows(0).ToString)

            Debug.Print FindID.ToString() & " " & ourBindingSource.Find("TempID", CSVRows(0).ToString)

            If FindID < 0 Then
                'If TemplateID does not exist
                ourBindingSource.AddNew()
            End If

            'Setting all checkboxes to indeterminate
            chkBox1.CheckState = CheckState.Indeterminate
            chkBox2.CheckState = CheckState.Indeterminate
            chkBox3.CheckState = CheckState.Indeterminate
            chkBox4.CheckState = CheckState.Indeterminate
            chkBox5.CheckState = CheckState.Indeterminate
            chkBox6.CheckState = CheckState.Indeterminate
            chkBox7.CheckState = CheckState.Indeterminate
            chkBox8.CheckState = CheckState.Indeterminate
            chkBox9.CheckState = CheckState.Indeterminate
            chkBox10.CheckState = CheckState.Indeterminate
            chkBox11.CheckState = CheckState.Indeterminate
            chkBox12.CheckState = CheckState.Indeterminate


            CType(ourBindingSource.Current, DataRowView)("TempID") = CSVRows(0).ToString
            If CSVRows(3).ToString = "Section 1" Then
                chkBox1.CheckState = CheckState.Checked
            ElseIf CSVRows(3).ToString = "Section 2" Then
                chkBox2.CheckState = CheckState.Checked
            ElseIf CSVRows(3).ToString = "Section 3" Then
                chkBox3.CheckState = CheckState.Checked
            ElseIf CSVRows(3).ToString = "Section 4" Then
                chkBox4.CheckState = CheckState.Checked
            ElseIf CSVRows(3).ToString = "Section 5" Then
                chkBox5.CheckState = CheckState.Checked
            ElseIf CSVRows(3).ToString = "Section 6" Then
                chkBox6.CheckState = CheckState.Checked
            End If
        Next
    End If

以下是我的CSV简介:

ID      SectionNumber
800     Section 1
800     Section 2
800     Section 4
800     Section 6
800     Section 3
800     Section 5
853     Section 3
853     Section 2
853     Section 1
853     Section 4
879     Section 2
879     Section 3
879     Section 4
879     Section 6
879     Section 5

那么 假设 的发生是我们搜索ID。如果它还不存在,我们将通过BindingSource添加一个新行,然后根据需要调整我们的复选框。如果 存在,那么我们会做同样的事情,除非我们不添加新行。

实际上发生的事情是,不仅复选框没有被正确标记,而且还有重复(总是在两个)行。因此,添加到我的数据集的行如下所示:
800个
800个
853个
853个
879个
879

实际上它应该是这样的:
800个
853个
879

我很困惑!有谁知道发生了什么?



修改
以下输出如下:

Debug.Print FindID.ToString() & " " & ourBindingSource.Find("TempID", CSVRows(0).ToString)
-1 -1
-1 -1
0 0
0 0
0 0
0 0
-1 -1
-1 -1
2 2
2 2
-1 -1
-1 -1
4 4
4 4
4 4



编辑2:我的theBindingSource

Dim dataset As New DataSet
WithEvents ourBindingSource As New BindingSource
'Dim ourBindingSource As New BindingSource
Dim ourDataView As New DataView

ourDataView = dataset.Tables("DefaultView").DefaultView
ourBindingSource.DataSource = ourDataView
DGV_blk.DataSource = ourBindingSource

'The sections are the SQL table headers
chkBox1.DataBindings.Add("CheckState", ourBindingSource, "Section1", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
chkBox2.DataBindings.Add("CheckState", ourBindingSource, "Section2", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
chkBox3.DataBindings.Add("CheckState", ourBindingSource, "Section3", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
chkBox4.DataBindings.Add("CheckState", ourBindingSource, "Section4", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
chkBox5.DataBindings.Add("CheckState", ourBindingSource, "Section5", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)
chkBox6.DataBindings.Add("CheckState", ourBindingSource, "Section6", True, DataSourceUpdateMode.OnValidation, CheckState.Indeterminate)

编辑3:我的怀疑
好吧,经过一些更多的修修补补,我仍然无法随心所欲,但我怀疑它是因为当它添加新记录时,它并没有提交它。这可能与脏编辑有关吗?

2 个答案:

答案 0 :(得分:0)

你不应该寻找

ourBindingSource.Find("ID", CSVRows(0).ToString)

不是TempID

答案 1 :(得分:0)

知道了!我只需要将private void pd_PrintPage(object sender, PrintPageEventArgs ev) { MessageBox.Show("Printing"); } 添加到.EndEdit