Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<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:
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):
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)

        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 9:24am

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900