Skip to content Skip to sidebar Skip to footer

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 = orderDataGridVie

Solution 1:

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();
    }
}

Solution 2:

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

Solution 3:

You can get the OrderID from the selected cell.

intselectedIndex= orderDataGridView.CurrentCell.RowIndex;
        StringselectedOrderID="";
        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();

Solution 4:

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();

Solution 5:

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

Post a Comment for "Delete Selected Row In Datagridview And Also Delete Row In Database"