2016-05-07 16 views
0

私のフォームのDataGridViewを使用して、CSV txtファイルからデータベースに情報をインポートする必要があります。アプリケーションでは、ユーザーが.txtファイルを開いて、自分のフォームでDataGridViewテーブルを更新できるようにする必要があります。ファイルを取得できましたが、ファイルを使用してグリッドを更新できません。私はテキストボックスを更新することはできますが、グリッドを更新する方法を理解することはできません。誰もがこれで私を助けることができますか?お使いのファイルの内容VBを使用してCSVファイルをデータベースにインポート

インポートSystem.IOでのGridViewを更新

Imports Microsoft.VisualBasic.FileIO 
Imports System.IO 

Public Class Form1 
Private fileToOpen As String    'the file to be opened and read 
Private responseFileDialog As DialogResult 'response from OpenFileDialog 
Private myStreamReader As StreamReader  'the reader object to get contents of file 
Private myStreamWriter As StreamWriter  'the writer object to save contents of textbox 
Private myTextFieldParser As TextFieldParser ' To parse text to searched. 
Dim myDataAdapter As OleDb.OleDbDataAdapter 
Dim myString() As String 
Dim myRow As DataRow 



Private Sub PeopleBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles PeopleBindingNavigatorSaveItem.Click 
    Me.Validate() 
    Me.PeopleBindingSource.EndEdit() 
    Me.TableAdapterManager.UpdateAll(Me.MyContactsDataSet) 

End Sub 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    'TODO: This line of code loads data into the 'MyContactsDataSet.People' table. You can move, or remove it, as needed. 
    Me.PeopleTableAdapter.Fill(Me.MyContactsDataSet.People) 

End Sub 

Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click 
    Dim fileContentString As String   'contents of the file 
    Dim update As New OleDb.OleDbCommandBuilder(myDataAdapter) 
    'Dim myRow As DataRow 

    'set the properties of the OpenFileDialog object 
    OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.CurrentDirectory 
    OpenFileDialog1.Title = "Select File to View..." 
    OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 


    'responseFileDialog contains holds the response of the user (which button they selected) 
    responseFileDialog = OpenFileDialog1.ShowDialog() 

    'check to see if the user select OKAY, if not they selected CANCEL so don't open anything 
    If (responseFileDialog <> System.Windows.Forms.DialogResult.Cancel) Then 
     'make sure there isn't a file already open, if there is then close it 
     If (myStreamReader IsNot Nothing) Then 
      myStreamReader.Close() 
      'TextBoxFileOutput.Clear() 
     End If 

     'open the file and read its text and display in the textbox 
     fileToOpen = OpenFileDialog1.FileName 
     myStreamReader = New StreamReader(OpenFileDialog1.FileName) 

     initTextFieldParser() 

     'loop through the file reading its text and adding it to the textbox on the form 
     Do Until myStreamReader.Peek = -1 
      fileContentString = myStreamReader.ReadLine() 

      'Try 
      ' myTextFieldParser = New TextFieldParser(fileToOpen) 
      ' myTextFieldParser.TextFieldType = FieldType.Delimited 
      ' myTextFieldParser.SetDelimiters(",") 
      'Catch ex As Exception 
      ' MessageBox.Show("Cannot Open File to Be Read!") 
      'End Try 


      myTextFieldParser.TextFieldType = FieldType.Delimited 

      myString = TextFieldParser.NewLine() 

      myRow.Item("FirstName") = myString(1) 

      MyContactsDataSet.Tables("People").Rows.Add(myRow) 
      PeopleTableAdapter.Update(MyContactsDataSet) 
      'TextBoxFileOutput.AppendText(fileContentString) 
      'TextBoxFileOutput.AppendText(Environment.NewLine) 
     Loop 

     'close the StreamReader now that we are done with it 
     myStreamReader.Close() 
     'SaveToolStripMenuItem.Enabled = True 
    End If 
End Sub 

Private Sub initTextFieldParser() 
    'Close myTextFieldParser in case the user is surfing through the records and then 
    'decides to search for a particular last name --> Basically  start searching from beginning of the file 
    If (myTextFieldParser IsNot Nothing) Then 
     myTextFieldParser.Close() 
    End If 

    Try 
     myTextFieldParser = New TextFieldParser(fileToOpen) 
     myTextFieldParser.TextFieldType = FieldType.Delimited 
     myTextFieldParser.SetDelimiters(",") 
    Catch ex As Exception 
     MessageBox.Show("Cannot Open File to Be Read!") 
    End Try 
End Sub 
End Class 
+0

は、あなたがしたコードを提供することができますこれまでに試してみて、うまくいきませんか? – Spidey

+0

私はちょうどそれを解散し、やり直しています。私はまもなく私が持っているものを入れます。ありがとうございました!! – Magdalina08

+0

詳細が不明です。ユーザーが保存前にデータ*を編集/表示できるようになっている場合は、空の(型付きの)DataTableにデータを解析し、DGVにバインドします。 csvテキストファイルを解析するには、TextFieldParser、OleDB、またはCSVHelperなどを使用します。ここまでの質問の数は、 – Plutonix

答えて

0

は、我々はやる必要があるとして、StreamReaderを

Using reader As New StreamReader("filepath") 
    DataGridView1.Columns.Add("col1",reader.ReadToEnd()) 
End Using 

チェックこのout!

関連する問題