|
-
Jun 13th, 2011, 06:18 AM
#1
Thread Starter
Lively Member
[RESOLVED] DataGridView save a new row or any changed cell
Hello.
I'm using a DGV and i'm looking for the best way to update the database whenever a new row is created OR whenever any datagrid view cell changes.
The following code works fine for new rows and my question is how can i adapt it to include ANY other changed cells of any other rows being or not new rows.
Code:
If Me.PECASDataGridView.CurrentRow.IsNewRow = True Then
Me.Validate()
Me.PECASBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.CV_PARTSDataSet)
End If
Many thanks in advance for any kind help.
Rgds,
Octavio
-
Jun 13th, 2011, 09:29 AM
#2
Re: DataGridView save a new row or any changed cell
The grid is basically irrelevant. You should be handling the appropriate events of the DataTable bound to the grid. Check out the MSDN documentation for the DataTable class to see what those events are.
-
Jun 13th, 2011, 09:36 AM
#3
Thread Starter
Lively Member
Re: DataGridView save a new row or any changed cell
Thank you for your answer. Sorry for bothering you but i'm new to this. I do not understand why the grid is irrelevant. That's where user is making changes before data is saved to database...
This is probably a stupid question but how can i check what has changed if i did not yet save the data...
-
Jun 13th, 2011, 11:44 AM
#4
Re: DataGridView save a new row or any changed cell
When you have a DGV, the data you see on your form must be bound to a DataTable. When you change, delete or add to it, the DataTable also triggers its events and can be handled by its methods. This is what jmcilhinney is talking about. The DataTable is not the physical data on your drive, its an object of your application which in some form is connected to the data on your drive. The DGV is not really relevant because the data is in fact handled by the DataTable.
Look into your code and find out what is the name of the TableAdapter and execute its Update method. ie:
MYTableAdapter.Update(MyDataSet.MyTable)
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Jun 13th, 2011, 11:50 AM
#5
Thread Starter
Lively Member
Re: DataGridView save a new row or any changed cell
Ho, that's much clear now, thank you. So instead of using the DataGridView object shal i use the DataTable object ? The DataTable object must be programatically defined, correct? Would you be so kind and give me a small example?
Thanks a lot.
Octavio
-
Jun 13th, 2011, 11:55 AM
#6
Re: DataGridView save a new row or any changed cell
It depends on how you implemented your DGV. Chances are you just dropped it from your DataSources, if that is the case, then you do not need to define anything, it is created by the UI. Look in your code, in the load form event and there must be a statement in which the tableAdapter is filled.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Jun 13th, 2011, 11:59 AM
#7
Thread Starter
Lively Member
Re: DataGridView save a new row or any changed cell
I think i got it. Thanks a lot once again.
Octavio
-
Jun 13th, 2011, 08:11 PM
#8
Re: [RESOLVED] DataGridView save a new row or any changed cell
The DataGridView is there for the user's sake, so that they can see the data and interact with it. In code, you should almost universally work with the data source, not the UI. One advantage you will find with that as you get more experience is that you can then make any changes you like to the UI without affecting that code. For instance, if you decided to use multiple TextBoxes to display one record at a time instead of a DataGridView to display them all, your code to handle this issue wouldn't change because the code doesn't involve the UI.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|