none
How to check if there are new/edited row(s) in DataGridView

    Question

  • Hi all,

    I have a tab control in my form. I've put different datagridviews into separate tabpages. All my datagridviews are bound to a bindingsource. I'd like the tableadapter to update, like auto-saves, whenever there are edited/inserted new rows in the datagridview. I've tried DataGridView.IsCurrentRowDirty but it doesn't saves the rows.
    Thanks in advanced for any reply.

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
            If addStatus = True Then
                'Autosaves when user moves to another tab
                SavetoDB()
                addStatus = False
            Else
                'MessageBox.Show("Position: " & Me.MAILBindingSource.Position & ", MailNo: " & Me.MAILNOTextBox.Text)
                Select Case Me.TabControl1.SelectedIndex
                    Case 0
                        'TODO: How to check if there are new/edited rows in datagridview?
                        If (Me.MAIL_INTERESTSDataGridView.IsCurrentRowDirty = True) Or (Me.MAILSOURCEDataGridView.IsCurrentRowDirty = True) Or (Me.MAILSTKDataGridView.IsCurrentRowDirty = True) Then 'addStatus = True Then
                            SaveAlsoToDB()
                            'Else
                            '    Me.MAIL_INTERESTSTableAdapter.FillByMailNo(Me.MaillistDataSet.MAIL_INTERESTS, Me.MAILNOTextBox.Text)
                        End If
                   ...
                End Select
            End If
           
        End Sub

    Public Sub SaveAlsoToDB()
            'Save the rest
            Me.Validate()
            'Mark end editing of all records
            Me.MAILSTKBindingSource.EndEdit()
            Me.MAILSOURCEBindingSource.EndEdit()
            Me.MAIL_INTERESTSBindingSource.EndEdit()
            'Me.MAILBindingSource.EndEdit()
            'Update all child records first, then parent records
            Me.MAILSTKTableAdapter.Update(Me.MaillistDataSet.MAILSTK)
            Me.MAILSOURCETableAdapter.Update(Me.MaillistDataSet.MAILSOURCE)
            Me.MAIL_INTERESTSTableAdapter.Update(Me.MaillistDataSet.MAIL_INTERESTS)
            'Me.MAILTableAdapter.Update(Me.MaillistDataSet.MAIL)

        End Sub
    Wednesday, January 17, 2007 8:30 AM

Answers

  • Maybe this will help:

    For Each dgvr As DataGridViewRow In MyDataGridView.Rows

       Dim drv As DataRowView = CType(dgvr.DataBoundItem, DataRowView)

       If drv IsNot Nothing Then Console.WriteLine(drv.Row.RowState.ToString())

    Next

     

    Wednesday, January 17, 2007 9:07 AM

All replies

  • Maybe this will help:

    For Each dgvr As DataGridViewRow In MyDataGridView.Rows

       Dim drv As DataRowView = CType(dgvr.DataBoundItem, DataRowView)

       If drv IsNot Nothing Then Console.WriteLine(drv.Row.RowState.ToString())

    Next

     

    Wednesday, January 17, 2007 9:07 AM
  • It sure did help. Thanks a lot!!
    Thursday, January 18, 2007 3:36 AM
  • I also have a binding navigator on that form. When I press the delete button, the record is not displayed to the user anymore. But when I refresh, it's still there.
    This also happened eventhough I press Save. The supposedly deleted record is not deleted from database. Why is this so? Please help.
    Thursday, January 18, 2007 3:45 AM
  • Check the TableAdapter for a valid Delete command?

    Thursday, January 18, 2007 7:18 AM
  • The TableAdapter generates insert/update/delete command. Insert and update is ok but not delete.

    But anyway, I've set the BindingNavigatorDeleteItem to nothing and put a custom code in there. Thanks again for your reply. :)

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
            'Delete record
     
                If (MessageBox.Show("Are you sure you want to delete Mail No: " & Me.MAILNOTextBox.Text & "?", "Confirm Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK) Then
                    Me.MAILBindingSource.RemoveCurrent()

                    Me.Validate()
                    Me.MAILBindingSource.EndEdit()
                    Me.MAILTableAdapter.Update(Me.MaillistDataSet.MAIL)

                End If
        End Sub
    Thursday, January 18, 2007 9:48 AM