Click here to Skip to main content
14,449,309 members
Rate this:
Please Sign up or sign in to vote.
I have a WPF app written using XAML and VB where I've used a Datagrid control to pull in my database data. The data pulls in great, and I can make changes within the cells as desired; however, those changes are not committed back to the database. I would like any changes made to save to the database as well.

Here is my XAML:
<DataGrid x:Name="RecordsDataGrid" CanUserAddRows="False" HorizontalAlignment="Left" Height="234" Margin="67,214,0,0" VerticalAlignment="Top" Width="1067" Background="#FFE0E0E0" AlternatingRowBackground="PaleGoldenrod" AlternationCount="2" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray"/>


Here is the VB:
Private Sub RecordsWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        '
        '   Get data from server
        '

        ' Create the SQL Connection variables
        Dim myConn As New SqlConnection

        ' Create the SQL Connection String
        [...]

        Dim query As String
        query = "SELECT id as 'ID', company as 'Company' FROM dB ORDER BY id"

        Dim command As New SqlCommand(query, myConn)
        command.CommandType = System.Data.CommandType.Text

        Dim dA As New SqlDataAdapter(command)
        Dim dT As New DataTable

        dA.Fill(dT)

        RecordsDataGrid.ItemsSource = dT.AsDataView

    End Sub


What I have tried:

I believe I can accomplish this via the UpdateCommand, but all solutions I have found are written in C# rather than VB (example):
Private Sub RecordsWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        '
        '   Get data from server
        '

        ' Create the SQL Connection variables
        Dim myConn As New SqlConnection

        ' Create the SQL Connection String
        [...]

        Dim query As String
        query = "SELECT id as 'ID', company as 'Company' FROM dB ORDER BY id"

        Dim command As New SqlCommand(query, myConn)
        command.CommandType = System.Data.CommandType.Text

        Dim dA As New SqlDataAdapter(command)
        Dim dT As New DataTable

        dA.Fill(dT)

        dA.Update(dT)

        RecordsDataGrid.ItemsSource = dT.AsDataView

    End Sub


But all conversions I have tried have not been successful.

Thank you in advance!
Posted
Updated 1-Oct-19 10:24am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If you want the data to go both ways, you need to bing to an UbservableCollection of objects that are derived from INotifyPropertyChanged.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100