Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How can I edit datagridview by importing csv file into it

How can I edit datagridview by importing csv file into it

Scheduled Pinned Locked Moved Visual Basic
databasequestionannouncement
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rajulama
    wrote on last edited by
    #1

    Hi, I m trying to develop a small application where I have to read the data from csv file, display it in DGV and update the database so that I can execute a query related to the database. My code is as follows:

    Public Class Form1
    Dim myDA As OleDbDataAdapter
    Dim myDataSet As DataSet

    Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\\db1.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT \* FROM Sheet1", con)
        con.Open()
        myDA = New OleDbDataAdapter(cmd)
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
        myDataSet = New DataSet()
        myDA.Fill(myDataSet, "MyTable")
        DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
        con.Close()
        con = Nothing
    End Sub
    Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Validate()
        Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
        Me.myDataSet.AcceptChanges()
    End Sub
    
    Private Sub DataGridView1\_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    
    End Sub
    
    Private Sub Button2\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\\db1.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT \* FROM Query3", con)
        con.Open()
        myDA = New OleDbDataAdapter(cmd)
        Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
        myDataSet = New DataSet()
        myDA.Fill(myDataSet, "MyTable")
        DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
        con.Close()
        con = Nothing
    End Sub
    
    Private Sub Button3\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            Dim fi As New FileInfo(OpenFileDialog1.FileName)
            Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName
            Dim Con As New OleDbConnection(sConnectionString)
            Dim myDataSet As D
    
    M 1 Reply Last reply
    0
    • R rajulama

      Hi, I m trying to develop a small application where I have to read the data from csv file, display it in DGV and update the database so that I can execute a query related to the database. My code is as follows:

      Public Class Form1
      Dim myDA As OleDbDataAdapter
      Dim myDataSet As DataSet

      Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\\db1.mdb")
          Dim cmd As OleDbCommand = New OleDbCommand("SELECT \* FROM Sheet1", con)
          con.Open()
          myDA = New OleDbDataAdapter(cmd)
          Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
          myDataSet = New DataSet()
          myDA.Fill(myDataSet, "MyTable")
          DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
          con.Close()
          con = Nothing
      End Sub
      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Me.Validate()
          Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
          Me.myDataSet.AcceptChanges()
      End Sub
      
      Private Sub DataGridView1\_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
      
      End Sub
      
      Private Sub Button2\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=|DataDirectory|\\db1.mdb")
          Dim cmd As OleDbCommand = New OleDbCommand("SELECT \* FROM Query3", con)
          con.Open()
          myDA = New OleDbDataAdapter(cmd)
          Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
          myDataSet = New DataSet()
          myDA.Fill(myDataSet, "MyTable")
          DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
          con.Close()
          con = Nothing
      End Sub
      
      Private Sub Button3\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
          If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
              Dim fi As New FileInfo(OpenFileDialog1.FileName)
              Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName
              Dim Con As New OleDbConnection(sConnectionString)
              Dim myDataSet As D
      
      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Other than to make you feel better, why are you using the DGV. It's nice to see the data but I would not include it in the process. I would do the following. Load the data from the CSV into a datatable Use Bulkcopy to push it into the database. This article may help[^] I would never use the jet oledb connection to read the data (just prejudiced I guess). Ah my mistake, you are using Access X| so the jet DB is already required.

      Never underestimate the power of human stupidity RAH

      R 1 Reply Last reply
      0
      • M Mycroft Holmes

        Other than to make you feel better, why are you using the DGV. It's nice to see the data but I would not include it in the process. I would do the following. Load the data from the CSV into a datatable Use Bulkcopy to push it into the database. This article may help[^] I would never use the jet oledb connection to read the data (just prejudiced I guess). Ah my mistake, you are using Access X| so the jet DB is already required.

        Never underestimate the power of human stupidity RAH

        R Offline
        R Offline
        rajulama
        wrote on last edited by
        #3

        can i use sqlCommand for access database also? I tried but it didnt support.

        D 1 Reply Last reply
        0
        • R rajulama

          can i use sqlCommand for access database also? I tried but it didnt support.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          No, you must use the OleDb classes, or for more generic support, the Odbc class (I don't recommend them) to get to Access database. The Sql classes only work with SQL Server.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          R 1 Reply Last reply
          0
          • D Dave Kreskowiak

            No, you must use the OleDb classes, or for more generic support, the Odbc class (I don't recommend them) to get to Access database. The Sql classes only work with SQL Server.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            R Offline
            R Offline
            rajulama
            wrote on last edited by
            #5

            so how can I use bulk copy command for access database

            D 1 Reply Last reply
            0
            • R rajulama

              so how can I use bulk copy command for access database

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              You can't. Access doesn't have such a facility.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups