Yuki
Yuki

Reputation: 43

XAML DataGrid update doesn't update database

I am trying to update database records from XAML DataGrid on RowEditEnding event DB name: ManageOrders Table: Customer: ID (Guid), Name (varchar), Address (varchar) Database set as : copy if newer Dataset set as : Do not copy

If i hard code my queries using UPDATE and ExecuteNonQuery i get the results but when i am trying to use datasets / dataTableAdapter.Update it will not update. Please help. Note im very new to WPF and C# programming. I spent a week researching this, cannot find an answer or solve it myself.

here is my code

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }


    private SimpleData.ManageOrdersDataSet manageOrdersDataSet;
    private SimpleData.ManageOrdersDataSetTableAdapters.CustomerTableAdapter manageOrdersDataSetCustomerTableAdapter;
    System.Windows.Data.CollectionViewSource customerViewSource; 

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        manageOrdersDataSet = ((SimpleData.ManageOrdersDataSet)(this.FindResource("manageOrdersDataSet")));
        // Load data into the table Customer. You can modify this code as needed.
        manageOrdersDataSetCustomerTableAdapter = new SimpleData.ManageOrdersDataSetTableAdapters.CustomerTableAdapter();
        manageOrdersDataSetCustomerTableAdapter.Fill(manageOrdersDataSet.Customer);
        customerViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("customerViewSource")));
        customerViewSource.View.MoveCurrentToFirst();
    }

    private void customerDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)        
{
   this.manageOrdersDataSetCustomerTableAdapter.Update(this.manageOrdersDataSet.Customer);
        manageOrdersDataSet.AcceptChanges();   

 }
}

here is my XAML

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SimpleData" x:Class="SimpleData.MainWindow"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

<Window.Resources>
    <local:ManageOrdersDataSet x:Key="manageOrdersDataSet"/>
    <CollectionViewSource x:Key="customerViewSource" Source="{Binding Customer, Source={StaticResource manageOrdersDataSet}}"/>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="533*"/>
        <ColumnDefinition Width="186*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="43*"/>
        <RowDefinition Height="377*"/>
    </Grid.RowDefinitions>
    <TabControl  Grid.Column="0" HorizontalAlignment="Left" Height="357" Margin="10,9.8,0,0" Grid.Row="1" VerticalAlignment="Top" Width="513">
        <TabItem x:Name="tabView" Header="Customer">
            <Grid DataContext="{StaticResource customerViewSource}">

                <DataGrid x:Name="customerDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="0,0,137,75" ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False" RowEditEnding="customerDataGrid_RowEditEnding" >
                    <DataGrid.Columns>
                        <DataGridTextColumn x:Name="iDColumn" Width="10" Header="ID" Binding="{Binding ID, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, Mode=TwoWay }"/>
                        <DataGridTextColumn x:Name="nameColumn" Width="*" Header="Name" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, Mode=TwoWay }"/>
                        <DataGridTextColumn x:Name="addressColumn" Width="*" Header="Address" Binding="{Binding Address, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, Mode=TwoWay}"/>
                    </DataGrid.Columns>
                </DataGrid>

            </Grid>
        </TabItem>

        <TabItem Header="Order">
            <Grid Background="#FFE5E5E5" Width="auto"/>
        </TabItem>
    </TabControl>


</Grid>

Upvotes: 0

Views: 269

Answers (1)

Yuki
Yuki

Reputation: 43

I found the answer to my problem. when I created my DB I forgot to set PRIMARY KEY. i mean that i didnt go to the properties and selected field - primary key (in MSSQL manager, design of tables) without setting that Visual studio could not build UPDATE command properly

to verify that i went to (RIGHT CLICK ON) TABLE ADAPTER -> CONFIGURE -> ADVANCE OPTIONS and check that REFRESH THE DATA TABLE is on.

hope this will help to someone one day.

Upvotes: 0

Related Questions