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;                  
                        da2.Fill(ds); 
                        cn.Open();
                        cmdDelete.ExecuteNonQuery();

Open in new window

wilko100Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
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.
0
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?
0
wilko100Author 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?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.