0

我想删除 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命令中;有人帮忙吗?

4

5 回答 5

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();
    }
}
于 2013-07-31T17:07:08.137 回答
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();
于 2014-03-12T09:09:20.800 回答
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();
于 2013-07-31T17:14:32.750 回答
0

我希望它可以帮助

dataGridView1.Rows.RemoveAt(item.Index);

顺便说一句,您应该使用事务范围来使这两个操作成为我的建议

于 2013-07-31T17:07:36.963 回答
0

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

于 2017-03-02T04:14:48.823 回答