'how to delete a record from the dataGridView control Dim dataSet As New DataSet Dim connString As String = "Data Source=localhost\SQLEXPRESS;Initial Catalog=customers-05;Integrated Security=True" Dim Conn As New SqlConnection(connString) Dim sqlString As String = "SELECT * from Customers ORDER BY Name" Dim dataAdapter As New SqlDataAdapter(sqlString, Conn) Const MSG_TITLE As String = "Laguna Cycle Works" Dim currentRowSelected As Integer = 0 Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '---set up the grid so they can edit by clicking on an item dgCustomers.EditMode = DataGridViewEditMode.EditOnEnter '---initially fill the grid from Customers Table GetMyData() End Sub Private Sub GetMyData() '---fill the grid with the data Try dataSet.Clear() dataAdapter.Fill(dataSet, "customers") dgCustomers.DataSource = dataSet dgCustomers.DataMember = "customers" Catch ex As Exception MsgBox("I could not read the database..." & Err.Description, MsgBoxStyle.Critical, "System Message") Finally Conn.Close() End Try End Sub Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteRecordToolStripMenuItem.Click '---note that the currentRowSelected is determined by the DataGrid_RowEnter event. See that event below. Try Dim customerName As String = "" 'this holds the customers name in the selected record Dim numRecords As Integer = 0 'how many records in the dataset table? numRecords = dataSet.Tables("customers").Rows.Count 'how many records? '---get the name from the cell in the selected row. Item = the actual contents of the cell customerName = dataSet.Tables("customers").Rows(currentRowSelected).Item(1).ToString '---don't let them select the row used for adding a new record If currentRowSelected <= numRecords - 1 Then If MessageBox.Show("Are you sure you want to delete " & customerName & "?", MSG_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then '---now whack it dataSet.Tables("customers").Rows(currentRowSelected).Delete() Dim cmd As New SqlCommandBuilder(dataAdapter) 'add delete/insert/edit commands to the dataAdapter dataAdapter.TableMappings.Clear() 'must do this or it tries to add my table twice dataAdapter.TableMappings.Add("Table", "customers") 'tell the DA we are adding a Table called customers dataAdapter.Update(dataSet, "customers") 'update the DB itself MsgBox("That customer record has been deleted and the database is updated.", MsgBoxStyle.Information, MSG_TITLE) End If Else MsgBox("Please select an existing record to delete.", MsgBoxStyle.Information, MSG_TITLE) End If Catch AnyErrors As Exception MsgBox("There was an error while deleting this record. " & Err.Description, MsgBoxStyle.Critical, MSG_TITLE) Exit Sub End Try GetMyData() End Sub Private Sub dgCustomers_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgCustomers.RowEnter currentRowSelected = e.RowIndex End Sub