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.
Related
I have a DataGridView that displays data from an SQL table. It is using sqldependency so that an alert is made when the data changes, and the DataGridView immediately displays the updated data. New rows will be added to this table regularly, and when this happens I want to take only those new rows from the first data grid and populate a separate DataGridView.
I've experimented a bit with RowAdded event, but it hasn't been entirely straightforward because it creates an alert for every row in the table whenever the grid view is updated by sqldependency.
Does anyone have experience doing something similar?
Let's call the first DataGridView with all the rows Grid A and the DataGridView that shows only new rows Grid B. Don't define the contents of Grid B by observing changes to Grid A. Instead, create your own definition of what a "new row" is. One easy way to do this is to make a copy of the contents of Grid A during every update (in the OnChange event of our SqlDependency object). Then, on the next update you'll be able to select rows into the "new rows" DataSet.
Here's the pseudo-code for the OnChange handler for your SqlDependency object (this should be watching the SqlCommand that fills Grid A):
Run a SqlCommand that selects rows from the Grid A datasource with an NOT IN clause that excludes rows with ID in a 2nd table called LastUpdate (see step 2). This will be the data source for Grid B
Run a SqlCommand that copies all rows of the Grid A data source to the LastUpdate table. It may be easiest to just delete the contents of LastUpdate first, then run a statement of the form: INSERT INTO LastUpdate(ID) SELECT ID FROM table_x
Update the Grid A normally
The point I want to emphasize is that this update strategy creates an effective separation between data queries and the UI. In other words, the data that drives Grid B does not in any way depend on Grid A. If you stick with this principle, you should wind up with a much more maintainable application.
I am inserting data into a DataTable() using table.Rows.Add(Val1,Val2... etc)
the problem is this table updates in real-time and I need to update the table values. I also have a cell style listener on the container for the table which detects when the value of a cell changes.
I dont want to re-draw the table as I get run-time errors complaining about the table constraints. Is there anyway to re-populate the table?
On a higher-level of thought, I cannot see how to do this without deleting the rows and then re-drawing them because it's possible that on the datatable reload a previous row may no longer exist in the latest database grab?
If you use {Binding}'s you wouldn't have to do anything except update the data source and the table would be automatically updated.
Here is one example but if you haven't done any Binding before I'd recommend reading up on it first, it is very powerful.
http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database
Hopefully simple, but can't find any such option.
I have a data table -- has say... 10 rows in it. Some fields on the form are bound to the table.columns respectively by name.
On another form that HAS a grid, as I scroll the grid, the detail fields are refreshed as expected since the grid does some magic to trigger the DataTable record changing event.
WITHOUT using a Data Grid, How can I direct the table to go to a specific row for load/display refresh on the form... ex:
DataTable MyTable = new DataTable();
MyTable = GetResultsFromSQL(); // returns the 10 rows
MyTable.LoadTheDataForRow(3);
MyTable.LoadTheDataForRow(7);
MyTable.LoadTheDataForRow(2);
I know I can use a foreach row in the table, but need explicit use as I don't want to go through all rows, but need specificity to specific ones.
I've looked at the LoadDataRow(), but that appears to be for pushing data back to a server. NOT what I want... I just want to have the "Current" row of the table to be of a specific one...
Thanks
After further research, I've found that a FORM based control "BindingSource" (or derivative) allows this, such as a grid. But, obviously, there's something the .Net engine is doing under the hood to ultimately "Load" a given row into something that ultimately triggers back to the "BindingSource"... The DataTable has RowChanging and RowChanged events which appear to be triggered by OnRowChanging / OnRowChanged delegates, but how can we tell the data table which "row" we want it as the active one.
The form controls can do this for their binding sources, but what is really happening under the hood to trigger these OnRowChanging events... I don't want to re-load a data table, rows, etc, just change what is considered the "Active" row, as in a grid, listbox, combobox, etc.
Have you tried:
MyDataTable.Rows[3];
MyDataTable.Rows[7];
MyDataTable.Rows[2];
I was under the impression that upon binding a dataGridView object to a dataTable that any changes the user makes in the dataGridView are then recorded in the dataTable. These changes include switches in column order, sorting, and the addition and deletion of rows. However, this does not seem to be the case. I am finding that changing column order, sorting, and occasionally the addition of rows is not reflected in the underlying dataTable. Are these changes that need to be made programmatically instead?
Thanks!
Yes, sorting and reordering columns are just different visual representations of the same data. This does NOT affect the underlying DataTable. Adding and deleteing rows should always work unless if there's some conflict like a primary key or something like that. Also, adding rows is only reflected in the DataTable after the user clicks out of the row in the DataGridView. The edit isn't committed up until that point.
I have a DataGridView bound to a LINQ to SQL query expression. I want it to be editable for updates, but for inserts I want users to use separate controls below the grid (textboxes, date pickers, etc - currently not bound to anything). I have an "Add" button that creates an object of the linq to sql entity class based on the values in the controls, and then calls InsertOnSubmit(). When I later call SubmitChanges(), any updates from the grid, and any objects added are all correctly persisted to the database.
My problem is that any new objects are not added to the grid, before or after the call to SubmitChanges(). I would like new objects to be added to the grid as soon as they are created. Also, I only want the controls below the grid to be used for inserting new records, not editing existing records, so I don't think they should be bound to the data source...What is the best way to make this happen?
I did try simply re-setting the DataSource for the grid (ie dataGridView.DataSource = db.<TableName>, which works, but is clumsy because it scrolls to the top of the grid again - I'm sure a better method exists.
(Pls excuse the n00b question, I'm very new to all this .net stuff :P)
The first thing to try is GetNewBindingList(), but this thread: "Linq-SQL canonical editable datagridview sample code" has some thoughts for other scenarios.