この場合、変換を手動で処理することをお勧めします。たとえば、DataAdapterとDataGridViewの間にプロキシクラスを作成して、それらが直接接続されないようにします。このようにして、DataAdapterはデータベースに保存し、変換を行うだけです。
編集:ここにあなたが始めるためのいくつかのアイデアがあります。以下のコードは、テスト目的で使用できるダミーテーブルを作成します。DataAdapterにはまったく依存しません。
Dim originalDataTable As New DataTable
With originalDataTable.Columns
.Add("Date", GetType(DateTime))
.Add("Sales", GetType(Integer))
End With
With originalDataTable.Rows
.Add({#12/3/2012#, 100})
.Add({#12/4/2012#, 50})
.Add({#12/6/2012#, 120})
End With
次に、必要な構造を準備します。このディクショナリが必要になるため、クラスレベルの変数を宣言することをお勧めします。おそらくあなたの場合はフォームです。
Dim salesDictionary As New Dictionary(Of Date, DataRow)
For Each row As DataRow In originalDataTable.Rows
salesDictionary.Add(row("Date"), row)
Next
Dim minDate As DateTime = salesDictionary.Keys.Min
Dim maxDate As DateTime = salesDictionary.Keys.Max
Dim newTable As DataTable = originalDataTable.Clone
Dim currentDate As DateTime = minDate
Do
Dim row As DataRow = Nothing
If salesDictionary.TryGetValue(currentDate, row) Then
newTable.ImportRow(row)
Else
newTable.Rows.Add({currentDate, Convert.DBNull})
End If
currentDate = currentDate.AddDays(1)
Loop While currentDate <= maxDate
newTable
の代わりにDataGridViewをバインドしoriginalDataTable
、変更をコミットする準備ができたら、次のoriginalDataTable
ように関連するレコードを手動で更新します。
For Each row As DataRow In newTable.Rows
Dim newSalesValue As Object = row("Sales")
Dim originalRow As DataRow = Nothing
If salesDictionary.TryGetValue(row("Date"), originalRow) Then
If newSalesValue Is Convert.DBNull Then
originalRow.Delete()
Else
originalRow("Sales") = row("Sales")
End If
ElseIf newSalesValue IsNot Convert.DBNull Then
originalDataTable.ImportRow(row)
End If
Next
次に、DataAdapterを使用してデータベースを更新します。