Delete from datagridview selected row

How you...
1) highlight a row in a datagridview
2) delete from both the gridview and the db

I have done most of the code but im struggling on the last bit that i commented on the code with a question mark
//Select from Contact table
                        SqlDataAdapter da2 = new SqlDataAdapter("SELECT * FROM Contact WHERE CustomerCounter = " + form.Counter + "", cn);                     
                        da2.TableMappings.Add("Table", "Contact"); 
                        //Delete from Contact table
                        SqlCommand cmdDelete = cn.CreateCommand();
                        cmdDelete.CommandType = CommandType.Text;
                        cmdDelete.ComandText = ("DELETE FROM Contact WHERE CustomerCounter = "+Counter from SelectedRow+"");//what can i use here??
                        da2.DeleteCommand = cmdDelete;                  

Open in new window

Who is Participating?
wilko100Connect With a Mentor Author Commented:
Can anyone help, im really struggling!! There must be a way of selecting then deleting the record in the db without altering the SQL statement. It deletes from the grid but not SQL. Actually i can get it to delete but it deletes everything, but thats why i need to know if there is something that detects which row the user selects?
I believe the approach above is a bit wrong.

You select using ""SELECT * FROM Contact WHERE CustomerCounter = " + form.Counter + """
This means that ALL records have the same CustomerCounter value.

Therefore the statement
"DELETE FROM Contact WHERE CustomerCounter = "+Counter from SelectedRow+"" will not work.
You need to find some other criteria for your delete statement.
wilko100Author Commented:
I have to have the select statement like this as the contacts are linked to customers using the customer counter, for example one customer may have 4 contacts against it. However the contacts do have a unique counter against each person.
 So what your saying is there is no way of destinguishing which record to delete? Im really new to datgridview. I thought there would be something that detects which row you have selected then delete Contact Where Counter = CustomerCunter from selectedRow?. So its all down to the SQL delete statement?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.