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. Datagrid and updating

Datagrid and updating

Scheduled Pinned Locked Moved C#
questioncsharpdatabasehelp
6 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.
  • M Offline
    M Offline
    mhmo
    wrote on last edited by
    #1

    Hi all I have a table that contains four attributes (testVersion, qnum, answer, weight). Now I want to display this table in a datagrid using C#. The user then should be able to change the value of the weight attribute of any record and then that value should be updated in the table. I am using MS Access database. How can I code it easily? I hope that u understand my question. please help me I should finish the project very soooooooooooooooon. thanks

    S 1 Reply Last reply
    0
    • M mhmo

      Hi all I have a table that contains four attributes (testVersion, qnum, answer, weight). Now I want to display this table in a datagrid using C#. The user then should be able to change the value of the weight attribute of any record and then that value should be updated in the table. I am using MS Access database. How can I code it easily? I hope that u understand my question. please help me I should finish the project very soooooooooooooooon. thanks

      S Offline
      S Offline
      Stanciu Vlad
      wrote on last edited by
      #2

      I think you have 2 ways to do waht you want : 1) make a button in the corner of the window whitch has the dataGrid that updates the database with the contnets of the grid or 2) catch every changed row in the dataGrid and update it in the database after the complete change I would recommand option 1 because when a mistake is made it can be undo. I hope you know something about asp.net in order to do what I wrote... ps: when a value is changed in a dataGrid, the dataTable that is the source is modified too. I hope you understand... By the way... visit http://nehe.gamedev.net[^]

      M 1 Reply Last reply
      0
      • S Stanciu Vlad

        I think you have 2 ways to do waht you want : 1) make a button in the corner of the window whitch has the dataGrid that updates the database with the contnets of the grid or 2) catch every changed row in the dataGrid and update it in the database after the complete change I would recommand option 1 because when a mistake is made it can be undo. I hope you know something about asp.net in order to do what I wrote... ps: when a value is changed in a dataGrid, the dataTable that is the source is modified too. I hope you understand... By the way... visit http://nehe.gamedev.net[^]

        M Offline
        M Offline
        mhmo
        wrote on last edited by
        #3

        would u provide me the code here. I visited the site but did not find any thing related. it is better if someone provide me the C# code with the MS Access.

        M 1 Reply Last reply
        0
        • M mhmo

          would u provide me the code here. I visited the site but did not find any thing related. it is better if someone provide me the C# code with the MS Access.

          M Offline
          M Offline
          mhmo
          wrote on last edited by
          #4

          This is my code to display the table into the datagrid. private void btnShow_Click(object sender, System.EventArgs e) { try { //set SQL query oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM KeyAnswers"; //clear dataset dataSet1.Clear(); //Fill dataset with information oleDbDataAdapter1.Fill(dataSet1,"KeyAnswers"); //bind datagrid dataGrid1.SetDataBinding(dataSet1,"KeyAnswers"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { } } This is when I click the show button. Now I want to create a "Update" button . what should be the code? Should I update the whole table wheras only one record has been changed?!! waitting..... thanks

          M 1 Reply Last reply
          0
          • M mhmo

            This is my code to display the table into the datagrid. private void btnShow_Click(object sender, System.EventArgs e) { try { //set SQL query oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM KeyAnswers"; //clear dataset dataSet1.Clear(); //Fill dataset with information oleDbDataAdapter1.Fill(dataSet1,"KeyAnswers"); //bind datagrid dataGrid1.SetDataBinding(dataSet1,"KeyAnswers"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { } } This is when I click the show button. Now I want to create a "Update" button . what should be the code? Should I update the whole table wheras only one record has been changed?!! waitting..... thanks

            M Offline
            M Offline
            mhmo
            wrote on last edited by
            #5

            pleasehelp me :(

            S 1 Reply Last reply
            0
            • M mhmo

              pleasehelp me :(

              S Offline
              S Offline
              Stanciu Vlad
              wrote on last edited by
              #6

              I think that when you click the show button the call of SetDataBindingis is not necesarry. And at the update you code should look like this : private void btnShow_Click(object sender, System.EventArgs e) { ... oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM KeyAnswers"; // This builds automaticly INSERT and UPDATE statement from the SELECT one // Use this once in the lifetime of the DataAdapter OleDbCommandBuilder cb = new OleDbCommandBuilder(oleDbDataAdapter1); ... } private void btnShow_Click(object sender, System.EventArgs e) { try { //Execute the command oleDbDataAdapter1.Update(dataSet1,"KeyAnswers"); MessageBox.Show("Changes saved successfully!"); } catch (Exception ex) { MessageBox.Show("There was an error while saving the changes : ex.ToString()); } finally { } } PS: my code is not 100% correc in the way i haven't got an .net compiler right now, but that is the algorith you shoul use. I hope you understand... By the way... visit http://nehe.gamedev.net[^]

              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