I am having problems getting a dataset to update changes to a database.
I start by filling a dataset using a FillBy query that I created on a table adapter.
"SELECT * FROM tblTest WHERE testID = (testID = #testId)
That will return a single record. I want to bind each column of the dataset.datatable to its own textbox on the form.
textBox1.DataBindings.Add("Text", dataset.datatable, dataset.datatable.NameColumn.ToString());
This loads the data fine. I make some changes to the data in the textbox. Then when I use the tableadapter.Update(dataset) to update the changes to the database, it does not update the record.
If I populate a datagrid with this dataset, make changes, and then run the tableadapter.update(dataset), it will update the record.
How can I use the tableadapter.update() without using the datagrid? Do I have to write a custom update for the tableadapter?
I did find out why the database was not updating. I checked the row state of the changed row and it was still in a "unmodified" state.
I changed the binding setup and created a new bindingsource with the datasource set as the dataset. and the datamember set as dataset.datatable. Then bound the control to a column in that bindingsource.
Before running the tableadapter.update(dataset) the following line is needed so that the row state changes
this.BindingSource.EndEdit();
Everything then updated properly
Related
I have a DataGridView on top of a TablePanelLayout. The DataGridView's data is bound on a database and using this code:
dgvItems.DataSource = GC.CSHR_SORepo.ToList();
Now, I have other methods that changes the values to the database table that the DataGridView references to. When that change happens, I can't refresh the values of the Grid.
I have tried dgvItems.Refresh(); tablePanelLayout.Refresh();, tablePanelLayout.RefreshEdit();
Even refreshing the form itself. Am I missing something?
I used the magic of c# to create a master detail form containing several datagridview controls. I have disabled editing and inserting rows to the datagrid view, and do it using a separate form instead. I take the user input from the form and call:
awardsTableAdapter.Insert(100, 5, "test", DateTime.Now);
This inserts and commits the row in the table, but doesn't refresh the datagridview.
Note the 100 is the foreign key to the master form, i use txtId.Text to get the value, if there is a better way to get the PK of the current record in the master table please let me know. Also please help on how to make the new row appear in the datagridview.
In order to get automatic updates in your DataGridView when the data has changed, you can use a BindingSource object.
Rather than wire up your DataGridView directly to the datasource do the following :-
BindingSource bs = new BindingSource();
bs.Datasource = YourDataSetObject;
Now you set the Datasource for your Datagrid as teh BindingSource object:-
dataGridView.Datasource = bs;
Hope this helps.
I have a situation where I have a grid loaded with data. It is not data
bound. Clicking an image opens up another form that allows the change of
the data in the grid.
Right now the changed data (1 row) is written to the DB and the whole
grid is reloaded from the DB which now incorporates the change.
My question is can I update the data in a data view? That way I can
right the change to the DB and update the DataView without having to dip
the DB every time and essentially avoid reloading a 1000 rows of data
because I changed one.
How do I change one row in a DataView?
This is not a generally supported feature, because it would only work if you were doing a straight select * from table query, with a known PK. Any time you had any joins, or aggregate functions involved etc, the entire query would need to be executed in order to get the value of that row.
Why aren't you using databinding? If your grid is bound to a datatable, and you do your update that way, then the grid would be automatically updated by virtue of being bound to the table.
Even if you do not want to do full round-trip databinding, if you still use the table, just update the table with the appropriate changes, and then re-bind the grid.
I need an example of code or project with one DataGridView Form and one DataTable so I can edit DataGridView cells and then all the changes are automatically commited into the DataTable. Also how can I create DataTable with the same columns with DataGridView other way that manually?
I tried to create DataGridView and DataTable with the same columns manually and then set the DataGridView.DataSource = DataTable, but data saved into the table are null (anyway number of new rows is correct, but values are invalid).
Tried to google it - all the samples and questions are about savind into DataBase, but I don't need a DB, I just need a simple DataTable and DataGridView binding.
Thanks!
There's no way to create a table automatically from the DataGridView columns (EDIT To refine this: There are ways to automatically create database structures - ORM would be a good keyword here - but I guess that's not what you need). The other way round works well: Create the table and assign it to the DataGridView as its data source. Then the grid should show the respective columns.
"Storing" the values in the data table works only if you have "told" the columns, which field of the underlying table they relate to. That is: You need to create the table columns (it has a name), and when you create the DataGridView column, you need to assign the column name to the DataPropertyName property of the column.
Easiest way for you: Create a typed dataset (use the "Add to project" dialog to add a new "DataSet"), manually create a table in the dataset's designer, drag an instance of your dataset to the form and assign this as the data source to your DataGridView. Everything else should appear automatically.
I hope you this links from codeproject might help:
http://www.codeproject.com/Questions/80935/save-datagrid-data-to-database
http://www.codeproject.com/Articles/12846/Auto-Saving-DataGridView-Rows-to-a-SQL-Server-Data
If your DataTable is set as the DataSource for the dataGridView, the contents in the dataGridView will reflect the data in the DataTable and vice versa. Therefore, if you make changes to the dataGridView, those changes are already in the DataTable.
Convert DatagridView To DataTable.
In C#.NET:
DataTable dt = ((DataView)this.dataGridView1.DataSource).Table;
VB.NET:
Dim dt As DataTable = CType(Me.dataGridView1.DataSource,DataView).Table
I'm very new to working with databases in C# (but not C# itself) and the whole concept of data binding, so please bear that in mind. I'm also using SQL Server CE if that affects this.
I'm trying to use a strongly-typed dataset built with VS2010 to access an SQL Server CE database, and bind WinForms controls to it to display data. I can get the controls bound fine, and they show the data that's currently in the database no problem. I can also insert new data into the database, and if I restart the application the inserted data shows up in the controls (specifically a ComboBox, but I'll be using more in the future).
The problem is that the new rows don't show up at all until I restart the app, and my understanding of data binding is that the controls should automagically update themselves. I've read about INotifyPropertyChanged but I'm not sure if it's the right thing to use here, and if it is, how do I use it for row insertions?
I'm binding the combobox like this:
DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
and inserting like this:
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
where Name is just a string pulled from a textbox.
Thanks.
EDIT: I should mention that the DB table I'm using is called tag, with two columns, tagID and tagname. tagID is a GUID, tagname is a varchar.
What you are doing with your TableAdpater.Insert is inserting your data directly into your database and you're circumventing any notifications in your application. If you do that then you have to do what iefpw said and that was to reload your datatable and rebind your controls.
An alternate method would be to add a row into your datatable. You can try something like this:
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
At this point you've added it to your DataTable and your combobox will automatically update, but the caveat is that it has not been written to your database so you'll need to make sure at some point you save to your database.
You should bind the data again and refresh the controls again like you did the first time. It doesn't just display the data. It is mechanic. It is not realtime.