Results 1 to 4 of 4

Thread: [RESOLVED] Datagridview to Access Table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Resolved [RESOLVED] Datagridview to Access Table

    Greetings, i have the following problem

    I am using VB 2005 and Access 2003

    I have on my app a DataGridView filled with data by a CSV file.

    Now i need this data transportet in to an existing table in my DB

    how can I do this?

    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datagridview to Access Table

    Use a DataAdapter to read the data from the CSV file into a DataTable. You can then use another DataAdapter to save the DataTable contents to the database, exactly as though it had come from there in the first place. Just note that the AcceptChangesDuringFill property of the first DataAdapter must be False, so that the rows are left in a state ready to be inserted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    Köln
    Posts
    395

    Resolved Re: Datagridview to Access Table

    I do not use a DataAdapter to get the Data from the CSV to the DataGridView

    Here the code i use to get the data from the CSV to the DataGridView

    Code:
    Dim dt As Data.DataTable
    dt = New DataTable 
    
    Dim y As Integer = 0 
    
    Using sr As New IO.StreamReader(strFileName)
       While Not sr.EndOfStream
             Dim splitValues() As String = sr.ReadLine.Split(splitChar)
             If y = 0 Then
                For x As Integer = 0 To splitValues.Length - 1
                   dt.Columns.Add(splitValues(x))
                Next
                y = 1
             Else
             Dim dr As DataRow = dt.NewRow 
             For x As Integer = 0 To splitValues.Length - 1
                dr(x) = splitValues(x)
             Next
    
             dt.Rows.Add(dr)
          End If
       End While
       dgvMain.Datasource = dt 
    End Using

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Datagridview to Access Table

    Quote Originally Posted by Bongo View Post
    I do not use a DataAdapter to get the Data from the CSV
    I'm suggesting that you should. You're populating a DataTable anyway and it would be less code with ADO.NET.

    Regardless, you DO have a DataTable so, as I said, you can simply use a DataAdapter to save the data to the database exactly as you would any other time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width