Delete the selected row from the DataGridView database.
# Region Delete selected rows in the DataGridView
// Click Delete.
Private void ButtonDelete_Click (object sender, EventArgs e)
{
If (maid = null | maid = null)
{
Return;
}
Else
{
If (this. dataGridView1.SelectedRows. Count> 0)
{
DialogResult dr = MessageBox. Show ("delete selected records", "prompt", MessageBoxButtons. OKCancel, MessageBoxIcon. Question );
If (dr = DialogResult. OK)
{
Try
{
Dbcon. OpenConn (); // open the Oracle connection
Foreach (DataGridViewRow row in this. dataGridView1.SelectedRows) // traverses the row of the selected dataGridView record
{
String strName = row. Cells [2]. Value. ToString (); // obtain the Value of the third column in dataGridView1
String SQL = string. Format ("delete from company where dname = '{0}'", strName); // SQL statement
OracleCommand cmd = new OracleCommand (SQL, conn );
Cmd. ExecuteNonQuery (); // execute the delete operation.
Cmd. Dispose ();
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString (), "prompt ");
}
Finally
{
Dbcon. CloseConn (); // close the Oracle connection
}
This. ButtonFresh_Click (null, null); // after deletion, perform the refresh operation // ButtonFresh is the name of the refresh button. The refresh method will not be written here.
}
Else
{
Return;
}
}
}
}
# Endregion
From floating clouds