Returning added rows - c#

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.

Related

How do I start the program off with multiple rows on a DataGridView?

I don't know think I wrote any code for this since the properties are all default. I don't want the user to have to add those by themselves when it is in the middle of the run.
It lets me add in columns but not rows in the designer.
Is there any property that can start the program with more than 1 row in the data grid view?
Populating the grid with data must be done with code. If you populate the grid in the Load event handler then the user will see that data when the form is displayed. You can add data directly to the grid or you can populate a DataTable or some other list and bind that to the grid.
If you're saying that you actually want multiple blank rows in the grid then you need to add those rows. The row you see by default is not actually part of the data of the grid. It's just there to indicate to the user that they can enter data.

DataTable / DataGridView Data Filter

I need to filter a data bound DataGridView when a CheckBox is checked/unchecked.
The filter is based on Completed/Uncompleted Records.
When I load the data into the underlying DataTable, I select all records from the Table.
When the application runs I want it to start up with only uncompleted records present.
When the user checks the check box it switches to completed tasks.
I have been able to do this with using DataView.RowFilter(), but the problem is that when ever a user marks an item as completed within the grid, it automatically removes it from view. (where the CheckBox is unchecked).
I only want the Data to filter as and when the CheckBox is checked/unchecked.
The one thing that I want to stay away from is re-loading the data from the database each time the CheckBox.checked event is fired, with a specific SQL statement.
I want to filter the Data in memory, but only as and when The user interacts with the CheckBox.
Any Suggestions welcome.
You could use DataTable.Select to filter the data instead of DataView.RowFilter and only update the Select filter when the checkbox check status changes.

How to update only one row in DataView from SQL database?

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.

Moving to specific record within a DataTable

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];

DataGridView bound to Linq to SQL does not show new rows added elsewhere to DB context

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.

Categories

Resources