The multi-row delete operation of listView is deleted from the database.
Private void buttonDelete_Click (object sender, EventArgs e)
{
DialogResult dr = MessageBox. Show ("are you sure you want to delete the selected record? "," Photonsoft ", MessageBoxButtons. OKCancel, MessageBoxIcon. Question );
If (dr = DialogResult. OK)
{
Try
{
Dbcon. OpenConn (); // open the Oracle connection.
String id = ""; // The id here is the ID (unique identification number) of my records in the database ).
Foreach (ListViewItem item in listView1.SelectedItems) // traverses the selected record.
{
For (int I = 0; I <listView1.SelectedItems. Count; I ++) obtains and deletes the id of each record by traversing the selected record.
{
If (this. listView1.SelectedItems [I]. Selected) // if row I is Selected, obtain the index number of the Selected row
{
Id = ds. tables ["notes"]. rows [this. listView1.SelectedIndices [I] [0]. toString (); // obtain the ID of the selected I-th record in the database.
String SQL = string. Format ("delete from notes where id = '{0}'", id); // delete a statement.
OracleCommand cmd = new OracleCommand (SQL, conn );
Cmd. ExecuteNonQuery (); // execute Delete.
Cmd. Dispose ();
}
}
}
ListView1.Refresh (); // refresh the listView after deletion.
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString (), "prompt", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Finally
{
Dbcon. CloseConn (); // close the Oracle connection.
}
}
Else
{
Return;
}
}
2. This operation is much easier if you only delete from the listView control rather than from the database. Follow these steps:
Foreach (ListViewItem item in this. listView1.SelectedItems)
{
If (item. Selected)
{
Item. Remove ();
}
}
From floating clouds