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

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.

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 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.

Periodically refreshing the data in a DataTable()?

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

Update a data row inside an asp.net Data Grid View

I think it would be easier for you to understand my problem if I explain the scenario a bit.
What I am doing is developing a system to a doctor using asp.net c#. For the creation of the prescription, I add a check box list for the list of medicines and submit button below it. When the submit button clicked selected check box list items added to the sql server database and show a data grid with the selected medicines in the same page.
In the data grid there are two columns for medicine name and dosage.Medicine name is taken from the database.I need to give the user the ability to enter the dosage through that grid view. I need to use update command because the record is already there in the sql server.But empty value in the dosage column.I tried to use update command in the data source control. But I can't figure out how to give the WHERE clause because I can't specify the database row which the updating record relate to.
I would be glad to hear some help from you. All the comments are welcome.
You need to set a parameter(s), please check this example to understand how it works. I guess, that you've added in the database some column that is unique identifier.. some autoincrement int or guid.

C# datatable, dictionary, datagridview

I have a project where I want to:
1) grab data from a sql server database,
2) pull the data into a c# program,
3) keep the data persistent - (store in a dictionary?)
4)view the data using a datagridview,
5) update the data, which would update the datagridview, the dictionary, and the database
What would be the most efficient way to implement this project?
My current thinking is to use a datatable to keep data persistent in program, and to allow data to be easily viewable. As well.
Any thoughts?
You can bind a DataGridView directly to a datasource (SQL Server) as described here
Create DBML, and use LinQ.
Get your data, and bind them to custom DataTable (which is created with your own column names, types etc.)
Bind your DataTable to gridView.
Put a Update button, and when user selects a row, clicks an update button, get selected row, Update this row with LinQ and refresh your gridView.
phsr's answer takes care of the UI and database. In order to store it locally you could use a SQL Express database, or an easy alternatively would be to simply store it in local XML files, see here for some help with that:
http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx
Hope that helps!

Categories

Resources