Im looking to Delete a row of information in my datagrid, but also from my database.
im not even really sure where to start.

I want to use a button to do this. so that when i highlight a row, then click the button it brings up a message box that asks " are you sure you want to delete row/s?" and then when you click yes it is deleted from both the grid and the database.

How are you filling the data grid? Are you doing a SELECT * or some other query?

How are you filling the data grid? Are you doing a SELECT * or some other query?

SELECT *

Ok, add something along the lines of SELECT * FROM table1 WHERE something=true

Then, when you click the button on your form, you can set 'something' = false in your database. Have you been able to connect to your database and update it? Or are you only using datagrids to read from it, and have done no writing?

Ok, add something along the lines of SELECT * FROM table1 WHERE something=true

Then, when you click the button on your form, you can set 'something' = false in your database. Have you been able to connect to your database and update it? Or are you only using datagrids to read from it, and have done no writing?

Right now i have it so that it will fill the data grid with the data i request, and i have it so that i can add new data.
I havnt worked on edit.
So yes i have been able to connect to my database and update it.

Take a look here. Let me know if you have any questions. After you do the UPDATE command, you'll need to call your Fill() method again, to repopulate the grid showing only the items that have 'something'=true

Are you using a datagrid or a datagridview?

Are you using a datagrid or a datagridview?

dataGridView

this is what i have for code so far.

private void button4_Click(object sender, EventArgs e)
        {
            int j = 1;
            int i = dataGridView1.CurrentRow.Index;
            string thisrow = "";

            thisrow = dataGridView1[j, i].Value.ToString();

            //this is to connect to my database
            DBconnect db = new DBconnect();
            string comm = new Company('D', thisrow).SetSql();
            OleDbConnection conn = db.DbConn(comm);

            OleDbCommand DEL = new OleDbCommand(comm, conn);
            conn.Open();

            int count = DEL.ExecuteNonQuery();
            conn.Close();

If you are using a datagridview then you can utilise the UserDeletingRow and UserDeletedRow events.

In the UserDeletingRow event you can display a warning asking if the user wishes to continue. If they click OK then allow the delete. If they click cancel then the EventArgs (e) have a property called cancel. If you set e.Cancel = True the delete will not occur.

In UserDeletedRow you can use e.Row to get the row that was deleted and fire off your SQL to delete the relevant row in the database.

You could optionally refresh your datagridview at this point. By reloading the data you ensure that it is correct. If the delete query completed correctly then there should be no change since the rowq was already deleted from the datagridview.

Hmm...just re-read your post. If you want to do it by button you'll need a different approach.
Just remembered, those events don't fire if you remove rows in code : /

The code you posted deletes the row from your database when the button is clicked...does that work correctly? If so, you just need to refresh your datagridview after the delete. Just find whatever code used to populate the grid BEFORE the delete and call it again :) It will refresh the data so the datagridview reflects the delete.

If you are using a datagridview then you can utilise the UserDeletingRow and UserDeletedRow events.

In the UserDeletingRow event you can display a warning asking if the user wishes to continue. If they click OK then allow the delete. If they click cancel then the EventArgs (e) have a property called cancel. If you set e.Cancel = True the delete will not occur.

In UserDeletedRow you can use e.Row to get the row that was deleted and fire off your SQL to delete the relevant row in the database.

You could optionally refresh your datagridview at this point. By reloading the data you ensure that it is correct. If the delete query completed correctly then there should be no change since the rowq was already deleted from the datagridview.

thanks, that worked perfect.

Add this to your button event

DataRow dr, drt;
                DataGridViewSelectedRowCollection myRow = dataGridView1.SelectedRows;
                DataGridViewRow row = dataGridView1.SelectedRows[0];
                int yourRowPosition = dataGridView1.SelectedRows[0].Index;
                foreach (DataGridViewRow r in myRow)
                {
                    int yourId = int.Parse(dtCommand.Rows[yourRowPosition][0].ToString());//or whatever the positon of id is
                    deleteEntryFromDB(yourId);
                    dr = (row.DataBoundItem as DataRowView).Row;
                    dtCommand.Rows.Remove(dr);
                }

where

public void deleteEntryFromDB(int idEntry)
        {
            cmd = new SqlCommand();
            cmd.CommandText = "delete from tblEntries where idEntry=" + idEntry;
            cmd.Connection = conn;
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.