简体   繁体   中英

Delete selected row in datagridview and also delete row in database

I want delete selected row in datagridview and also delete row in Mysql.

private void deleteOrderButton_Click(object sender, EventArgs e)
{
    int selectedIndex = orderDataGridView.CurrentCell.RowIndex;

    if (selectedIndex > -1)
    {
        orderDataGridView.Rows.RemoveAt(selectedIndex);
        orderDataGridView.Refresh();
    }

    string constring = "datasource=localhost;port=3306;username=admin;password=acw123";
    string Query = "delete from database.tem_order where temp_orderID = ????
    MySqlConnection conDatabase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
    MySqlDataReader myReader;

    conDatabase.Open();
    myReader = cmdDataBase.ExecuteReader();
    MessageBox.Show("Updated");

I stuck in MySql command there; someone help?

When using a datasource on the DataGridView you can retreive the object of the selected row.

DataRow row = (dataGridView.SelectedRows[0].DataBoundItem as DataRowView).Row;

The DataRow 'row' should contain the ID which allows you to delete the record in MySQL. Replace "ID-column-name" with the real column name

using(MySqlConnection sqlConn = new MySqlConnection("datasource=localhost;port=3306;username=admin;password=acw123"))
{
    sqlConn.Open();

    using(MySqlCommand sqlCommand = new MySqlCommand("DELETE FROM tem_order WHERE temp_orderID = " + row["ID-column-name"],sqlConn))
    { 
        sqlCommand.ExecuteNonQuery();
    }
}

I hope it could help

dataGridView1.Rows.RemoveAt(item.Index);

By the way You should use transaction Scope to make those two actions my advice

You can get the OrderID from the selected cell.

        int selectedIndex = orderDataGridView.CurrentCell.RowIndex;
        String selectedOrderID = "";
        if (selectedIndex > -1)
        {

            orderDataGridView.Rows.RemoveAt(selectedIndex);
            //Replace "OrderID" with the column name of the Order's ID
            selectedOrderID = orderDataGridView.Rows[selectedIndex].Cells["OrderID"].Value.ToString();

        }

Your query will be like this:

        string Query = "delete from database.tem_order where temp_orderID = " + selectedOrderID ;
        //This is only an example, you should set the params to avoid SQL Injection

Then you just delete and refresh the gridview, done:

        conDatabase.Open();
        myReader = cmdDataBase.ExecuteReader();
        orderDataGridView.Refresh();
DialogResult dr = XtraMessageBox.Show(Global.lk, "Are you sure to Delete this record ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (dr.ToString() == "Yes")
            {
                int[] selectedRows = gridViewDeduction.GetSelectedRows();
                string type_id = gridViewDeduction.GetRowCellValue(selectedRows[0], gridViewDeduction.Columns["TypeId"]).ToString();


                if (type_id != "")
                {
                    string xsql = "DELETE FROM Pay_DeductionTypeMaster WHERE TypeId = " + type_id + " AND CompanyID = " + Global.CompanyID + "";
                    bool del = Database.ExecuteSQL(xsql);
                    if (!del)
                    {
                        XtraMessageBox.Show(Global.lk, "Unable to delete record.This deduction details is in use.", "Deduction Type");                          
                        return;
                    }
                    dt.Rows.RemoveAt(gridViewDeduction.FocusedRowHandle);
                }
                else
                    dt.Rows.RemoveAt(gridViewDeduction.FocusedRowHandle);
                dt.AcceptChanges();
                gridViewDeduction.RefreshData();

("delete from TableName where ID = ('"+Grid1.Rows[Grid1.CurrentRow.Index].Cells["ID"].Value+"');"); //get the value of selected cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM