updating dataset through datagrid
-
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.
-
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.
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.
-
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.
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?
-
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?
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?
-
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?
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); }
-
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); }
-
I think that dataGrid1.SetDataBinding(dvm2, null); is wrong. try: dataGrid1.DataSource=dvm2; What method is FilterView?
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; }
-
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; }
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.
-
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; }
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?