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. updating dataset through datagrid

updating dataset through datagrid

Scheduled Pinned Locked Moved C#
csharpdatabasesql-serverwpfwcf
9 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.
  • Z Offline
    Z Offline
    zuhx
    wrote on last edited by
    #1

    I have a datagrid, dataset, and sql server data source. I would like to be able to update my dataset through my datagrid. I thought binding the dataset to the datagrid would give me the ability to do this, but I guess I am wrong (I am new to C# and .NET). I have created a dataAdapter to update the datasource, but I would like to update the dataset first. Can any one help me get started here. What I have is a datagrid and would like to allow the user to do update, insert, and delete data then click a button that will sync the datagrid with the dataset. Any insight is appreciated.

    S 1 Reply Last reply
    0
    • Z zuhx

      I have a datagrid, dataset, and sql server data source. I would like to be able to update my dataset through my datagrid. I thought binding the dataset to the datagrid would give me the ability to do this, but I guess I am wrong (I am new to C# and .NET). I have created a dataAdapter to update the datasource, but I would like to update the dataset first. Can any one help me get started here. What I have is a datagrid and would like to allow the user to do update, insert, and delete data then click a button that will sync the datagrid with the dataset. Any insight is appreciated.

      S Offline
      S Offline
      STW
      wrote on last edited by
      #2

      Binding a Control to a DataTable or DataSet adjusts only Data in the DataSet NOT in the SQL-Server Table. That means that your input in your Datagrid will only change Data in the DataSet. You have to Update the Tables with the DataAdapter.Update(DataTable) / SqlDataAdapter.Update(...) Method. So your Button to Update: SqlDataAdapter.Update(DataTable) /DataSet DataTable dt=new DataTable("Table1"); .. .. SqlDataAdapter ada=new SqlDataAdapter(...); ada.Update(dt); Be aware of not to call DataSet.AcceptChanges/DataTable.AcceptChanges() before DataAdapter.Update()! because otherwise no data will be updated; after AcceptChanges all Rows are marked as unchanged and so the DataAdapter don't "find" Rows to Update! In the help files everywhere you can find "AcceptChanges()" which is very irritating.

      Z 1 Reply Last reply
      0
      • S STW

        Binding a Control to a DataTable or DataSet adjusts only Data in the DataSet NOT in the SQL-Server Table. That means that your input in your Datagrid will only change Data in the DataSet. You have to Update the Tables with the DataAdapter.Update(DataTable) / SqlDataAdapter.Update(...) Method. So your Button to Update: SqlDataAdapter.Update(DataTable) /DataSet DataTable dt=new DataTable("Table1"); .. .. SqlDataAdapter ada=new SqlDataAdapter(...); ada.Update(dt); Be aware of not to call DataSet.AcceptChanges/DataTable.AcceptChanges() before DataAdapter.Update()! because otherwise no data will be updated; after AcceptChanges all Rows are marked as unchanged and so the DataAdapter don't "find" Rows to Update! In the help files everywhere you can find "AcceptChanges()" which is very irritating.

        Z Offline
        Z Offline
        zuhx
        wrote on last edited by
        #3

        Thanks for the response, but I realize that binding a control to a dataset will only update the dataset. The problem is after i bind to the control and update the datagrid.....the data in the dataset is not being modified. is there any code i need to implement to get the dataset updaated with the the data the user has modified in the datagrid?

        S 1 Reply Last reply
        0
        • Z zuhx

          Thanks for the response, but I realize that binding a control to a dataset will only update the dataset. The problem is after i bind to the control and update the datagrid.....the data in the dataset is not being modified. is there any code i need to implement to get the dataset updaated with the the data the user has modified in the datagrid?

          S Offline
          S Offline
          STW
          wrote on last edited by
          #4

          Now I think I understand your question: You modify data in the datagrid by typing in some new text, numbers...but the datasets data isn'nt changed which has nothing to do with the Sql-Server side datatables. If a Control is bound to a dataset, the datasets data is changed when the user changes data in the control at once. That means that your binding code may be wrong. Would you like to show me your binding code?

          Z 1 Reply Last reply
          0
          • S STW

            Now I think I understand your question: You modify data in the datagrid by typing in some new text, numbers...but the datasets data isn'nt changed which has nothing to do with the Sql-Server side datatables. If a Control is bound to a dataset, the datasets data is changed when the user changes data in the control at once. That means that your binding code may be wrong. Would you like to show me your binding code?

            Z Offline
            Z Offline
            zuhx
            wrote on last edited by
            #5

            Here is the code for my data binding. The form has a datagrid and two drop down lists. One of the lists allows the user to choose the table to modify and click a button to retrieve the data in that table. Therefore, when the form initially loads, I only bind it to the dataset and not a particular datatable. After the user chooses a datatable from the drop down list, I have an event handler that updates the data binding. Thanks for any help. The code below is the initial data binding where the control is bound to only a dataset: this.dataGrid1.SetDataBinding(todv.dvm, null) I call another object that creates and manipulates a dataviewmanager (todv). todv.Filterview returns a dataview. So in this case, I bind to a dataview. The event handler to update the databinding: private void retrieveBtn_Click(object sender, System.EventArgs e) { string selectedItem = comboBox1.SelectedItem.ToString(); DataView dvm2 = todv.FilterView(comboBox2.Text.ToString(),comboBox1.Text.ToString()); dataGrid1.SetDataBinding(dvm2, null); }

            S 1 Reply Last reply
            0
            • Z zuhx

              Here is the code for my data binding. The form has a datagrid and two drop down lists. One of the lists allows the user to choose the table to modify and click a button to retrieve the data in that table. Therefore, when the form initially loads, I only bind it to the dataset and not a particular datatable. After the user chooses a datatable from the drop down list, I have an event handler that updates the data binding. Thanks for any help. The code below is the initial data binding where the control is bound to only a dataset: this.dataGrid1.SetDataBinding(todv.dvm, null) I call another object that creates and manipulates a dataviewmanager (todv). todv.Filterview returns a dataview. So in this case, I bind to a dataview. The event handler to update the databinding: private void retrieveBtn_Click(object sender, System.EventArgs e) { string selectedItem = comboBox1.SelectedItem.ToString(); DataView dvm2 = todv.FilterView(comboBox2.Text.ToString(),comboBox1.Text.ToString()); dataGrid1.SetDataBinding(dvm2, null); }

              S Offline
              S Offline
              STW
              wrote on last edited by
              #6

              I think that dataGrid1.SetDataBinding(dvm2, null); is wrong. try: dataGrid1.DataSource=dvm2; What method is FilterView?

              Z 1 Reply Last reply
              0
              • S STW

                I think that dataGrid1.SetDataBinding(dvm2, null); is wrong. try: dataGrid1.DataSource=dvm2; What method is FilterView?

                Z Offline
                Z Offline
                zuhx
                wrote on last edited by
                #7

                STW wrote: try: dataGrid1.DataSource=dvm2; This is not possible as the DataSource property takes a string. Tried different ways to assign dvm2 to DataSource but did not work. STW wrote: dataGrid1.SetDataBinding(dvm2, null); But this is working. I guess the question is, if I bind to a dataview and then make changes to data through the datagrid will the underlying datatable be modified as well. It seems like it should. Here is the FilterView method. It just simply adds a filter on dataset: public DataView FilterView(string dataTable, string filter) { DataView newView = dvm.CreateDataView(tom.tradarOmsMapping.Tables[dataTable]); newView.RowFilter = "Oms like '"+filter+"'"; return newView; }

                S 2 Replies Last reply
                0
                • Z zuhx

                  STW wrote: try: dataGrid1.DataSource=dvm2; This is not possible as the DataSource property takes a string. Tried different ways to assign dvm2 to DataSource but did not work. STW wrote: dataGrid1.SetDataBinding(dvm2, null); But this is working. I guess the question is, if I bind to a dataview and then make changes to data through the datagrid will the underlying datatable be modified as well. It seems like it should. Here is the FilterView method. It just simply adds a filter on dataset: public DataView FilterView(string dataTable, string filter) { DataView newView = dvm.CreateDataView(tom.tradarOmsMapping.Tables[dataTable]); newView.RowFilter = "Oms like '"+filter+"'"; return newView; }

                  S Offline
                  S Offline
                  STW
                  wrote on last edited by
                  #8

                  No, the DataSource Property of a DataGrid needs an object which can be DataSet, DataTable, DataView. So now I think now I understand your question. Yes, all changes made in a datagrid will change the underlying DataTable, View. When you then Update via an DataAdapter, the new data will be written to the Sql-Table. The DataGrid is synchronised with the Table, View. But now I'm not anymore sure if I understood your problems with the grid. 1. When you change your two lists is the correct Data shown in the DataGrid? 2. When you changed Data by writing in the DataGrid and update it, is the new Data written to the Sql-Server? If you answer the two question with Yes I don't understand the problem.

                  1 Reply Last reply
                  0
                  • Z zuhx

                    STW wrote: try: dataGrid1.DataSource=dvm2; This is not possible as the DataSource property takes a string. Tried different ways to assign dvm2 to DataSource but did not work. STW wrote: dataGrid1.SetDataBinding(dvm2, null); But this is working. I guess the question is, if I bind to a dataview and then make changes to data through the datagrid will the underlying datatable be modified as well. It seems like it should. Here is the FilterView method. It just simply adds a filter on dataset: public DataView FilterView(string dataTable, string filter) { DataView newView = dvm.CreateDataView(tom.tradarOmsMapping.Tables[dataTable]); newView.RowFilter = "Oms like '"+filter+"'"; return newView; }

                    S Offline
                    S Offline
                    STW
                    wrote on last edited by
                    #9

                    No, DataGrid.DataSource needs an object which can be a DataSet, Table, View, ...You can read this in the help. Yes, the DataGrid is "equal" to the Table. When you change Data in the Grid, the Table will change at once, too. Two questions: 1. When you make changes to the two lists you have, is the correct Data shown in the Grid? 2. When you Update the Tables, DataSet is the new data written to Sql-Server?

                    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