問題を解決しようとしていますが、今まで失敗しています
データグリッド ビューは BindingSource にバインドされていますが、データグリッド ビューから行を削除すると、update(table) コマンドが実行されますが、データベースは変更されません。
このコードで datagridview をロードしています
Private Sub loadSalueUnits(ByVal itemName As String)
table.Clear()
If (itemName <> "0") Then
ItemID = db.GetVal("select RawItemId from tblRawItem where RawItemName='" & itemName & "'")
dsSaleUnit = db.GetData("SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & "")
Else
ItemID = "0"
End If
loadComboBox()
'SaleUnit
If (ItemID = "0") Then
Try
Dim objNetworksetting As New NetworkSetting
Dim source As String = objNetworksetting.ReadConnectionXML()
Dim connectionString As String = source
Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & ""
Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource.DataSource = table
data.Locale = System.Globalization.CultureInfo.InvariantCulture
dgvSaleUnit.DataSource = Me.bindingSource
SaleUnit.DataPropertyName = "Unit"
UnitDifference.DataPropertyName = "UnitDifference"
For i As Integer = 0 To table.Rows.Count - 1
dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
Next
Me.dgvSaleUnit.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)
dgvSaleUnit.Columns(2).Visible = False
dgvSaleUnit.Columns(3).Visible = False
dgvSaleUnit.Columns(4).Visible = False
dgvSaleUnit.Columns(5).Visible = False
dgvSaleUnit.Columns(6).Visible = False
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
ElseIf dsSaleUnit.Tables(0).Rows.Count <> 0 Then
Try
Dim objNetworksetting As New NetworkSetting
Dim source As String = objNetworksetting.ReadConnectionXML()
Dim connectionString As String = source
Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & ""
Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource.DataSource = table
data.Locale = System.Globalization.CultureInfo.InvariantCulture
dgvSaleUnit.DataSource = Me.bindingSource
SaleUnit.DataPropertyName = "Unit"
UnitDifference.DataPropertyName = "UnitDifference"
salePrice.DataPropertyName = "SalePrice"
For i As Integer = 0 To table.Rows.Count - 1
dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
Next
Me.dgvSaleUnit.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)
dgvSaleUnit.Columns(2).Visible = False
dgvSaleUnit.Columns(3).Visible = False
dgvSaleUnit.Columns(4).Visible = False
dgvSaleUnit.Columns(5).Visible = False
dgvSaleUnit.Columns(6).Visible = False
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
そして、このコードで datagridiew update から行を削除した後
Dim objNetworksetting As New NetworkSetting
Dim source As String = objNetworksetting.ReadConnectionXML()
Dim connectionString As String = source
Dim con As New OdbcConnection(connectionString)
con.Open()
Dim selectCommand As String = "SELECT * from tblItemSaleUnits"
Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
table.Locale = System.Globalization.CultureInfo.InvariantCulture
dataAdapter.Fill(table)
dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand()
table = Me.bindingSource.DataSource
table.AcceptChanges()
Me.dataAdapter.Update(table)
実行しますが、データベースに変更を加えません