Multi-row deletion of the DataGridView in Winform

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.