DataGridView1.EndEdit() For Each gridRow As DataGridViewRow In DataGridView1.SelectedRows If Not gridRow.IsNewRow Then 'IsNewRow 判定するかは御好みで DirectCast(gridRow.DataBoundItem, DataRowView).Delete() End If Next
Dim bnd = Me.BindingContext(Me.DataGridView1.DataSource, Me.DataGridView1.DataMember) Dim rowView = DirectCast(bnd.Current, DataRowView) If rowView IsNot Nothing Then rowView.Delete() End If
Dim gridRow = DataGridView1.CurrentRow If gridRow IsNot Nothing Then Dim rowView = TryCast(gridRow.DataBoundItem, DataRowView) If rowView IsNot Nothing Then rowView.Delete() End If End If
Dim rows = From cell As DataGridViewCell In DataGridView1.SelectedCells Let rowView = DirectCast(cell.OwningRow.DataBoundItem, DataRowView) Where rowView IsNot Nothing Select rowView Distinct For Each rowView In rows rowView.Delete() Next
'パラメーター化された DELETE 文を構築 Dim sql As String = "DELETE FROM T_ITEM WHERE ITEM_ID = @OLD_ITEM_ID" Dim delCmd As New OleDb.OleDbCommand(sql, conn) '列の型に合わせること Dim oldId As MySqlParameter = delCmd.Parameters.Add("@OLD_ITEM_ID", MySqlDbType.VarChar, 8) 'DataViewRowState を指定して、削除マークの付いた行を取り出す Dim deletedRows As New DataView(dt, "", "", DataViewRowState.Deleted) For Each rowView As DataRowView In deletedRows Dim row As DataRow = rowView.Row 'DataRowVersion を指定して、該当行の削除前の値を取り出す Dim OLD_ITEM_ID As Object = row("ITEM_ID", DataRowVersion.Original) '削除前の主キー値を使って DELETE 文を実行 oldId.Value = OLD_ITEM_ID delCmd.ExecuteNonQuery() Next