我想在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命令那里; 有人帮忙吗?
当在DataGridView中使用的数据源,你可以retreive所选行的对象。
DataRow row = (dataGridView.SelectedRows[0].DataBoundItem as DataRowView).Row;
DataRow中“行”应包含允许你删除在MySQL中记录的ID。 与真正的列名称替换“ID列名称”
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();
}
}
我希望它可以帮助
dataGridView1.Rows.RemoveAt(item.Index);
顺便说一句,您应该使用事务范围,使这两个动作我的建议
您可以从选定单元格获得订单ID。
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();
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();
( “从表名删除其中ID =( ' ”+ Grid1.Rows [Grid1.CurrentRow.Index] .Cells [“ ID ”]值+。“');”); //获取选定单元格的值