none
BindingNavigatorDeleteItem_Click always deletes the first row and the selected row. RRS feed

  • Question

  • Visual Studio 2012 vb

    Friend WithEvents dgvTimeSheet2 As System.Windows.Forms.DataGridView
     
    Dim dvTimeSheet As New DataView()
    dvTimeSheet.Table = Me.DsTimeSheet2.TimeSheet
    dgvTimeSheet2.DataSource = dvTimeSheet

    Code in the Delete Item event:
    dgvTimeSheet2.Rows.Remove(dgvTimeSheet2.CurrentRow)

    In the save function:
    Dim TimeSheetDeletes() As DataRow = Me.DsTimeSheet2.TimeSheet.Select("", "", DataViewRowState.Deleted)
    Me.TimeSheetTableAdapter.Update(TimeSheetDeletes)

    The above line will alway delete the first row in the datagridview and the selected row.
    If the code is removed from the BindingNavigatorDeleteItem_Click , only the first row will be deleted.
    How can the DataViewRowState of the first row be changed from DataViewRowState.Deleted to DataViewRowState.Unchanged?

    Or how does just the selected row get deleted without deleting the first row?
    In the datagridview properties MultiSelect is set to false.


    Bill

    Wednesday, July 19, 2017 8:38 PM

Answers

  • Hello,

    You should use a BindingSource, set the DataSource property to the DataView or DataTable. For the BindingNavigator, set the BindingSource property to the BindingSource just mentioned.

    The BindingSource will be the data source for the DataGridView.

    So now you can remove the current row in the DataGridView via RemoveCurrent of the BindingSource.

    Source where Sample3 class loads data from a database table

    Public Class BindingNavExample
        Private bs As New BindingSource
    
        Private Sub BindingNavExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New Sample3
            ' load customers
            ops.LoadCustomers()
            ' get customers
            bs.DataSource = ops.CustomersDataTable.AsDataView
            DataGridView1.DataSource = bs
            BindingNavigator1.BindingSource = bs
        End Sub
        Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) _
            Handles BindingNavigatorDeleteItem.Click
    
            If bs.Current IsNot Nothing Then
                Dim companyName = CType(bs.Current, DataRowView).Row.Field(Of String)("CompanyName")
                If My.Dialogs.Question($"Remove {companyName}") Then
                    bs.RemoveCurrent()
                End If
            End If
        End Sub
    End Class
    Namespace My
        <ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
        Partial Friend Class _Dialogs
            Public Function Question(ByVal Text As String) As Boolean
                Return (MessageBox.Show(Text, Application.Info.Title,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
        End Class
        <HideModuleName()>
        Friend Module KarensDialogs
            Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
            ReadOnly Property Dialogs() As _Dialogs
                Get
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace

    For the Navigator I remove the default behavior under properties for the BindingNavigator

    In action


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, July 19, 2017 9:04 PM
    Moderator

All replies

  • Hello,

    You should use a BindingSource, set the DataSource property to the DataView or DataTable. For the BindingNavigator, set the BindingSource property to the BindingSource just mentioned.

    The BindingSource will be the data source for the DataGridView.

    So now you can remove the current row in the DataGridView via RemoveCurrent of the BindingSource.

    Source where Sample3 class loads data from a database table

    Public Class BindingNavExample
        Private bs As New BindingSource
    
        Private Sub BindingNavExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ops As New Sample3
            ' load customers
            ops.LoadCustomers()
            ' get customers
            bs.DataSource = ops.CustomersDataTable.AsDataView
            DataGridView1.DataSource = bs
            BindingNavigator1.BindingSource = bs
        End Sub
        Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) _
            Handles BindingNavigatorDeleteItem.Click
    
            If bs.Current IsNot Nothing Then
                Dim companyName = CType(bs.Current, DataRowView).Row.Field(Of String)("CompanyName")
                If My.Dialogs.Question($"Remove {companyName}") Then
                    bs.RemoveCurrent()
                End If
            End If
        End Sub
    End Class
    Namespace My
        <ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
        Partial Friend Class _Dialogs
            Public Function Question(ByVal Text As String) As Boolean
                Return (MessageBox.Show(Text, Application.Info.Title,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
        End Class
        <HideModuleName()>
        Friend Module KarensDialogs
            Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
            ReadOnly Property Dialogs() As _Dialogs
                Get
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace

    For the Navigator I remove the default behavior under properties for the BindingNavigator

    In action


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, July 19, 2017 9:04 PM
    Moderator
  • Many thanks for the quick response!

    The issue was in the property setting in the BindingNavigator. When this setting was changed from DeleteItem = BindingNavigatorDeleteItem to DeleteItem = None, the first row is no longer deleted. 

    The sample code and snap shot of the setting has been very helpful!


    Bill

    Thursday, July 20, 2017 1:22 PM