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. C#
  4. Update changes made in gridview in DataTable

Update changes made in gridview in DataTable

Scheduled Pinned Locked Moved C#
databasequestionsalesannouncement
7 Posts 2 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.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I have a sql database with data about customers. I made it possible to add/change/remove customers from the gridview. The customer's data are stored in a sql database. Suppose the user adds/changes/removes a row/cell in my gridview. How can i update the changes made in the gridview in the datatable? The idea behind all this is to update the sql database as much as possible 'automatically'. This is a peace of my code: dbCommand.CommandText = "SELECT firstName, LastName FROM Customer"; customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString); reader = dbCommand.ExecuteReader(); // Populate a new data table and bind it to the BindingSource. customerTable = new System.Data.DataTable(); customerAdapter.Fill(customerTable); grdcustomerManagement.DataSource = customerTable; Any idea's?

    D 1 Reply Last reply
    0
    • Y Yustme

      Hi, I have a sql database with data about customers. I made it possible to add/change/remove customers from the gridview. The customer's data are stored in a sql database. Suppose the user adds/changes/removes a row/cell in my gridview. How can i update the changes made in the gridview in the datatable? The idea behind all this is to update the sql database as much as possible 'automatically'. This is a peace of my code: dbCommand.CommandText = "SELECT firstName, LastName FROM Customer"; customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString); reader = dbCommand.ExecuteReader(); // Populate a new data table and bind it to the BindingSource. customerTable = new System.Data.DataTable(); customerAdapter.Fill(customerTable); grdcustomerManagement.DataSource = customerTable; Any idea's?

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

      Use the same DataAdapter and call its Update method.

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

      Y 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Use the same DataAdapter and call its Update method.

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

        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        Hi, I tried that, but it's giving me an error (which is in dutch) about 'delete command'. I used the update method of the data adapter in the Rowsremoved event of the gridview.

        D 1 Reply Last reply
        0
        • Y Yustme

          Hi, I tried that, but it's giving me an error (which is in dutch) about 'delete command'. I used the update method of the data adapter in the Rowsremoved event of the gridview.

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

          Ah. You need to create a CommandBuilder object on that DataAdapter so it can fill in the missing SQL insert, delete, and update queries that the adapter needs. Or, you could just add those Command objects to the DataAdapter yourself.

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

          Y 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Ah. You need to create a CommandBuilder object on that DataAdapter so it can fill in the missing SQL insert, delete, and update queries that the adapter needs. Or, you could just add those Command objects to the DataAdapter yourself.

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

            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            Hi, Any chance you can give me a code sample? I've tried a few i found on the net. But no luck.

            D 1 Reply Last reply
            0
            • Y Yustme

              Hi, Any chance you can give me a code sample? I've tried a few i found on the net. But no luck.

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

              There is a single line of code you add to the creation of the DataAdapter to do this. I cannot believe you didn't find an example that explains this.

              dbCommand.CommandText = "SELECT firstName, LastName FROM Customer";
              customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString);
              Dim cb = new SqlCommandBuilder(customerAdapter);
              customerTable = new System.Data.DataTable();
              customerAdapter.Fill(customerTable);

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

              Y 1 Reply Last reply
              0
              • D Dave Kreskowiak

                There is a single line of code you add to the creation of the DataAdapter to do this. I cannot believe you didn't find an example that explains this.

                dbCommand.CommandText = "SELECT firstName, LastName FROM Customer";
                customerAdapter = new SqlDataAdapter(dbCommand.CommandText, connectionString);
                Dim cb = new SqlCommandBuilder(customerAdapter);
                customerTable = new System.Data.DataTable();
                customerAdapter.Fill(customerTable);

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

                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                Hi, I was actually talking about how to update the datatable after a few rows have been removed. I already got those lines in my application. I tried updating the datatable with customerAdapter.Update() method. But it's complaining about a deleteCommand. So after some rows have been removed from the gridview which i caught in the event handler of the datagridview, how do i update the datatable and update the sql database with the customerAdapter.Update() method? [EDIT] Ah never mind, i see what i was doing wrong. I forgot to put the adapter inside the () of the commandbuilder instance. Thank you, it works perfectly now! [/EDIT]

                modified on Friday, December 12, 2008 2:37 AM

                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