删除datagridview中的选定行,并删除数据库中的行

时间:2013-07-31 16:53:04

标签: c# mysql datagridview delete-row sql-delete

我希望在datagridview中删除所选行,并删除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");

我坚持使用MySql命令; 有人帮忙吗?

5 个答案:

答案 0 :(得分:1)

在DataGridView上使用数据源时,您可以检索所选行的对象。

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

DataRow'row'应包含允许您删除MySQL中记录的ID。 将“ID-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();
    }
}

答案 1 :(得分:0)

我希望它可以提供帮助

dataGridView1.Rows.RemoveAt(item.Index);

顺便说一下你应该使用事务范围将这两个动作作为我的建议

答案 2 :(得分:0)

您可以从所选单元格中获取OrderID。

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

        }

您的查询将如下:

        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

然后你只需删除并刷新gridview,完成:

        conDatabase.Open();
        myReader = cmdDataBase.ExecuteReader();
        orderDataGridView.Refresh();

答案 3 :(得分:0)

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

答案 4 :(得分:0)

(“从TableName中删除ID =('”“+ Grid1.Rows [Grid1.CurrentRow.Index] .Cells [”ID“]。Value +”');“); //获取所选单元格的值