スティーブの要求ごとにデータテーブルに書き込む前に、行の状態で改訂:は、SQLテーブルに更新されませんので、変更されたとしてマークされていません
私は、あるデータテーブルへの書き込み以下のコードを持っていますフォーム上のSAVEボタンを押すことから呼び出されます。テーブルの変更は最終的にはデータアダプタを介してSQLデータベースに送られますが、最初の行はデータベースに正しく保存されず、最初の行の状態がデータテーブルに「変更されていません」と表示されています。 コンソールの書き込みでステートメントをラップして、何が起こっているのかを確認しました。誰も私にそれを書いた後に変更された最初の行をマークしていない理由を私に説明することはできますか?行の残りの部分が行います。
データテーブルに書き込むためのコード:コードの下
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
は、データベースを更新するために、すぐに上記のコードの後に、ちょうどSQL文の前に呼ばれると呼ばれるサブルーチンです:
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
の眺め出力ウィンドウ:
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
ご協力いただきありがとうございます私に多くの助けをされている!
最初のループでもRowStateの出力を追加できますか? – Steve
奇妙な、私はデータテーブルの周りを移動する単純なコードでこの動作を再現することができません – Steve
私はこのdatatableにソースdatagridviewを持っています。しかし、私が得られないことは、ループが実行されている間に何が起こるかということです。行は明らかに値を変更するので、どのように状態を変更できないのですか?データテーブル内で変更を加える前に、データグリッドビューの値を変更していましたが、それも問題の原因と思われました。 – Forfun