How to make datagridview automatically save a record when exiting a row

I have a datagridview I want to automatically save each record after editing this record.

+3
source share
3 answers

There is an event RowValidatedsuitable for this. Then you can infer the values ​​from the grid and populate the database command.

+4
source

The following works great for me:

 ' ListChanged event is called whenever binding or datatable changes
Private Sub ProjectBindingSource_ListChanged(sender As Object, e As ListChangedEventArgs) Handles ProjectBindingSource.ListChanged

    Select Case e.ListChangedType
        ' Only update when listchanges are of following types
        Case ListChangedType.ItemChanged, ListChangedType.ItemAdded, ListChangedType.ItemDeleted
            Try
                'Update method will select the required update/insert/delete sql according to the tableadapter settings,
                'after successful update, it will accept changes in the dataset and change status of all rows back to unchanged
                Me.ProjectTableAdapter.Update(Me.StuklijstDataSet.Project)

            Catch ex As Exception
                ' exceptions will bounce back from the server,
                ' this will handle all possible data problems: concurrency, foreign key constraints, null not allowed, ... ;
                ' the update method (see above) will also mark the row with an ErrorProvider icon and tooltip with the error message

                ' if update is unsuccessful, roll back changes to the datatable
                Me.StuklijstDataSet.Project.RejectChanges()

                ' resetbindings to sort out problems with the control when rolling back adding or deleting records
                Me.ProjectBindingSource.ResetBindings(False)
            End Try

    End Select

End Sub

3-4 , Listchangedtype (< 1 ). RaiseListChangedEvents bindingsource, False ListChanged, , -, datatable. / . .

. , SqlException, SQL ErrorProvider .

+1

Source: https://habr.com/ru/post/1755470/


All Articles