Reputation: 81
Revised with row state before writing to datatable per Steve's request:
I have the below code writing to a datatable which is called from hitting a SAVE button on the form. The changes in the table are eventually sent to a SQL database through a dataadapter, but the first row isn't saving in the database correctly and I have found it's because the first row's state is showing "Unchanged" in the datatable. I wrapped the statement with console writes to see what is going on. Can anyone explain to me why its not marking the first row as modified after I write to it? The rest of the rows do.
Code to write to datatable:
Console.WriteLine(DateTime.Now.ToString & " *************** New Entry ***************")
'update datetime in the datatable
Dim rowindex As Integer = 0
For Each row As DataRow In dt_PartData.Rows
If Not row.RowState = DataRowState.Deleted Then
Console.WriteLine(rowindex.ToString & " Row state Before: " & row.RowState.ToString)
Console.WriteLine(rowindex.ToString & " Reading Before: " & dt_PartData.rows(rowindex)("LastSaved").ToString)
dt_PartData.rows(rowindex)("LastSaved") = DateTime.Now.ToString
Console.WriteLine(rowindex.ToString & " Writing: " & DateTime.Now.ToString)
Console.WriteLine(rowindex.ToString & " Reading After: " & dt_PartData.rows(rowindex)("LastSaved").ToString)
Console.WriteLine(rowindex.ToString & " Row state After: " & row.RowState.ToString)
End If
rowindex = rowindex + 1
Next row
Below code is a called subroutine called immediately after the above code and just before the SQL statement to update the database:
Console.WriteLine(DateTime.Now.ToString & " *************** Saving Entry ***************")
'update datetime in the datatable
Dim rowindex As Integer = 0
For Each row As DataRow In dt_PartData.Rows
If Not row.RowState = DataRowState.Deleted Then
Console.WriteLine(rowindex.ToString & ": Row state: " & row.RowState.ToString)
End If
rowindex = rowindex + 1
Next row
A view of the output window:
10/11/2016 9:44:17 AM *************** New Entry *************** In 10min
0 Row state Before: Unchanged
0 Reading Before: 10/11/2016 7:41:27 AM
0 Writing: 10/11/2016 9:44:30 AM
0 Reading After: 10/11/2016 9:44:30 AM
0 Row state After: Unchanged
1 Row state Before: Unchanged
1 Reading Before: 10/11/2016 9:39:55 AM
1 Writing: 10/11/2016 9:44:30 AM
1 Reading After: 10/11/2016 9:44:30 AM
1 Row state After: Modified
2 Row state Before: Unchanged
2 Reading Before: 10/11/2016 9:39:55 AM
2 Writing: 10/11/2016 9:44:30 AM
2 Reading After: 10/11/2016 9:44:30 AM
2 Row state After: Modified
3 Row state Before: Unchanged
3 Reading Before: 10/11/2016 9:39:55 AM
3 Writing: 10/11/2016 9:44:30 AM
3 Reading After: 10/11/2016 9:44:30 AM
3 Row state After: Modified
4 Row state Before: Unchanged
4 Reading Before: 10/11/2016 9:39:55 AM
4 Writing: 10/11/2016 9:44:30 AM
4 Reading After: 10/11/2016 9:44:30 AM
4 Row state After: Modified
5 Row state Before: Unchanged
5 Reading Before: 10/11/2016 9:39:55 AM
5 Writing: 10/11/2016 9:44:30 AM
5 Reading After: 10/11/2016 9:44:30 AM
5 Row state After: Modified
6 Row state Before: Unchanged
6 Reading Before: 10/11/2016 9:39:55 AM
6 Writing: 10/11/2016 9:44:30 AM
6 Reading After: 10/11/2016 9:44:30 AM
6 Row state After: Modified
7 Row state Before: Unchanged
7 Reading Before: 10/11/2016 9:39:55 AM
7 Writing: 10/11/2016 9:44:30 AM
7 Reading After: 10/11/2016 9:44:30 AM
7 Row state After: Modified
10/11/2016 9:44:30 AM *************** Saving Entry ***************
0: Row state: Unchanged
1: Row state: Modified
2: Row state: Modified
3: Row state: Modified
4: Row state: Modified
5: Row state: Modified
6: Row state: Modified
7: Row state: Modified
Thanks for your help ahead of time, this forum has been a lot of help to me!
Upvotes: 2
Views: 766
Reputation: 81
I have it working now. I have found two solutions that seemed to work for this.
So I added this line of code before making changes to the datatable:
BindingContext(dt_PartData).EndCurrentEdit()
Or this:
2. Setting the datagridview.DataSource to nothing and then make changes to the datatable, then put the .DataSource back to the datatable.
DataGridViewPartData.DataSource = Nothing
(make changes to the datatable here)
DataGridViewPartData.DataSource = dt_PartData
Upvotes: 2