1

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?

2
  • 1
    How is the datasource of the grid set? Commented Jul 31, 2013 at 17:00
  • the datasource was from same table, i retrive out to display and i want to delete selected item in datagrid and also in database Commented Jul 31, 2013 at 17:04

5 Answers 5

2

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

how about the query to delete the row?
Take a look at the anwser again, i've added a sample for the delete query.
error occur Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Do you have a selected row?
0

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

3 Comments

RemoveAt doesn't give an ID back which he needs to perform an MySQL delete.
i want to ask what shoud i type in database query string Query = "delete from database.tem_order where temp_orderID = ????
use for do loop to get selected row and assign like int ordrID = YourDatagrid.Rows[i].Cells[c].Value; and your Query = "delete from database.tem_order where temp_orderID = ordrID
0

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

2 Comments

string selectedOrderID = 0; Cannot implicitly convert type 'int' to 'string'
Sorry, my bad, it must be: String selectedOrderID = "";
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();

Comments

0

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

1 Comment

Please format it correctly and add some description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.