Solved

Is it possible to update a DataTable existing rows?

Posted on 2014-10-10
2
96 Views
Last Modified: 2014-10-11
I'm using a DataTable to handle detail records. The flow of logic is that a user retrieves a 1 Header Record and 1 or more Detail Records from the Database. The Detail Records are loaded into a DataGridView. Each time the user selects a Detail Record from the DGV, it loads the data into various controls on the Windows Form. What I would like to do is simply assign each DataColumn to each control and then just Update or Refresh the DataRow and then the DataTable. I'm pretty sure this can be done but not exactly sure how to execute it.

Thanks!
0
Comment
Question by:BlakeMcKenna
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40374192
Yes it can be done.


When they click the save button you would have to:

1)  Find the DataRow in the DataTable:
Dim dr as DataRow
' find the specific row.  I'm matching the ID column in the datatable to a label id which you need to populate so you can keep track yourself.

dr = myDataTable.Rows.OfType(DataRow).Where(Function(r) r("ID") = lblID.Text).Single()

Open in new window


2) edit the row:
dr.BeginEdit()
dr("Field1") = txt1.Text
dr("field2") = ddl1.SelectedValue
'etc
dr.EndEdit

Open in new window


Note that this saves it to the datatable.  You would have to push this back to the database using a dataadapter or looping over the rows.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40374938
Thanks Kyle...that did the trick!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now