0
    i used this code to delete row from datagridview but it dosnt delete the selected row it delete the first row , where is the problem??


    Dim sCommand As SqlCommand
    Dim sAdapter As SqlDataAdapter
    Dim sBuilder As SqlCommandBuilder
    Dim sDs As DataSet
    Dim sTable As DataTable



*****

  Dim connectionString As String = "Data Source=HABOUSH-PC\SQLEXPRESS;Initial Catalog=TenderingSystem;Integrated Security=True"
        Dim sql As String = "SELECT * FROM BOQDetailEntry"
        Dim connection As New SqlConnection(connectionString)
        connection.Open()
        sCommand = New SqlCommand(sql, connection)
        sAdapter = New SqlDataAdapter(sCommand)
        sBuilder = New SqlCommandBuilder(sAdapter)
        sDs = New DataSet()
        sAdapter.Fill(sDs, "BOQDetailEntry")
        sTable = sDs.Tables("BOQDetailEntry")

        connection.Close()

        BOQDetailEntryDGV.DataSource = sDs.Tables("BOQDetailEntry")


**********



 Dim i As Integer
        i = BOQDetailEntryDGV.CurrentRow.Index
        If MessageBox.Show("Are You sure you want to delete " + BOQDetailEntryDGV(4, i).Value().ToString + " Entry ?", "Warning Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.OK Then
            Try
                BOQDetailEntryDGV.Rows.RemoveAt(BOQDetailEntryDGV.CurrentRow.Index)
                sAdapter.Update(sTable)
                '*******************************************&&&&********************************
            Catch ex As Exception
                MessageBox.Show("No record deleted")
            End Try
        End If
2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by G_Waddell
0

What selection mode is your datagrid using? If it is rowselect I think you will have use SelectedRows instead of CurrentRow, CurrentRow gets the row of the current selected cell but if you are using the rowselect mode you have not selected a cell but a row and so you will always get a zero.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.