Periodically refreshing the data in a DataTable()? - c#

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

Related

Wrong row gets deleted in DataGridView bound to DataAdapter

Situation:
I'm writing a Winforms app using C# in VS2013 with .NET4.0.
I have a datagridview which I bind to a DataAdapter linked to a MySQL table. I'm performing updates using the DataAdapter's Update method. Due to the unconventional handling of dates and booleans in the MySQL table I set my own SQL commands rather than using CommandBuilder. These are fed by parameters set before calling Update. I call Update on RowValidated.
For inserting and amending this works fine.
Issue:
In the datagridview, when the user selects a single row, I want them to be able to hit delete and have the row removed from both the grid and the database. When I try this the row disappears from the datagridview and of course the cursor moves to a different row. But the database row that gets deleted is the new datagridview row that the cursor has fallen on. This is almost certainly because I use datagridview.CurrentRow to set the parameter for the delete command and I assume that at this stage the current row has indeed changed.
Question:
How do I amend this arrangement so the delete will work correctly? Clearly the underlying issue is that the row has disappeared from the grid before the database update takes place but I'm not sure how to resolve that. I suspect the solution may lie in handling things through different events but I'm not sure which.
Ok generating the SQL Delete command doesn't work when I reference CurrentRow from the dgv because by the time I'm building my parameters and doing the adapter.Update that dgv row has gone. That's why I end up deleting the wrong row. So I've solved it like this:
In the UserDeletingRow event I'm storing the primary key value for the row being deleted.
In RowValidated, where I call the adapter.Update I first use the stored primary key to build the correct SQL Delete command. Even though the row has already gone from the dgv the database delete now works correctly.
Relieved to get this working but this does seem like a pretty inelegant solution. Grateful if anyone knows of a slicker way of handling this. I'd be intrigued to know just what logic the CommandBuilder would have used. (Sadly I can't use CommandBuilder because my Inserts and Updates need to do Unix_TIMESTAMP conversions).

How to update Database Table when multiple rows are edited in Gridview

I have inserted the screenshot of my Windows Form which pulls up information from a Product Table:
Now as you can see i have allowed the option to Modify and then Save the modifications in the Database Table. So the issue i am facing is in updating the data in the Database Table. Since the user can update more than 1 row at a time in the GridView, how do i update the Database table?
I can either do a row-wise update, or i can loop through the GridView and update my table and then use a Command Builder.
I want know how do i achieve the solution in both scenarios.
I would loop through the rows and pass the values to a parameter and then update in database. One thing, it is more work that doesn't need to be done. But another thing that helps is you know whatever cell they edit will be committed to the database.

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.

Limitations to recording changes in a bound dataGridView to a dataTable

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.

Cleanest way to implement collapsable entries in a table generated via asp:Repeater?

Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of a very large system, so it really wouldn't be wise for me to revise the table structure (the app is filled with similar tables).
This is a webapp in C# .NET - data comes in from a webservice and is displayed onscreen in a table. The table's rows are generated with asp:Repeaters, so that the rows alternate colers nicely. The table previously held one item of data per row. Now, essentially, the table has sub-headers... The first row is the date, the second row shows a line of data, and all the next rows are data rows until data of a new date comes in, in which case there will be another sub-header row.
At first I thought I could cheat a little and do this pretty easily to keep the current repeater structure- I just need to feed some cells the empty string so that no data appears in them. Now, however, we're considering one of those +/- collapsers next to each date, so that they can collapse all the data. My mind immediately went to hiding rows when a button is pressed... but I don't know how to hide rows from the code behind unless the row has a unique id, and I'm not sure if you can do that with repeaters.
I hope I've expressed the problem well. I'm sure I'll find a way TBH but I just saw this site on slashdot and thought I'd give it a whirl :)
When you build the row in the databinding event, you can add in a unique identifier using say the id of the data field or something else that you use to make it unique.
Then you could use a client side method to expand collapse if you want to fill it with data in the beginning, toggling the style.display setting in Javascript for the table row element.
just wrap the contents of the item template in an asp:Panel, then you have you have a unique id. Then throw in some jquery for some spice ;)
edit: just noticed that you are using a table. put the id on the row. then toggle it.

Categories

Resources