Yuki
Yuki

Reputation: 43

RowEditEnding and DataGrid update

I have WPF dataGrid that; when I am updating my Database using RowEditEnding event it does not function properly.

  1. I enter new value, in the code I assign ID and when I'm back to Grid I do not see my ID but only value I typed. (if I close application, data are lost).
  2. When I double click on datagrid again and leave row everything works as it should have in the first place, data get updated and saved in DB.

Am I using the right method?

Here is my code:

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

<Grid DataContext="{StaticResource customerViewSource}">
    <DataGrid x:Name="customerDataGrid"
              RowDetailsVisibilityMode="VisibleWhenSelected"
              Margin="0,0,-0.2,0.4" ItemsSource="{Binding}"
              EnableRowVirtualization="True" AutoGenerateColumns="False"
              RowEditEnding="customerDataGrid_RowEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="iDColumn" Width="10" Header="ID"
                                Binding="{Binding ID,  Mode=TwoWay,
                                NotifyOnTargetUpdated=True,
                                NotifyOnSourceUpdated=True,
                                UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn x:Name="nameColumn" Width="*" Header="Name"
                                Binding="{Binding Name, Mode=TwoWay,
                                NotifyOnTargetUpdated=True,
                                NotifyOnSourceUpdated=True,
                                UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn x:Name="addressColumn" Width="*" Header="Address"
                                Binding="{Binding Address, Mode=TwoWay,
                                NotifyOnTargetUpdated=True,
                                NotifyOnSourceUpdated=True,
                                UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
OrderManager.ManageOrdersDataSet manageOrdersDataSet;
OrderManager.ManageOrdersDataSetTableAdapters.CustomerTableAdapter
manageOrdersDataSetCustomerTableAdapter;
System.Windows.Data.CollectionViewSource customerViewSource;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    manageOrdersDataSet = ((OrderManager.ManageOrdersDataSet)
        (this.FindResource("manageOrdersDataSet")));
    // Load data into the table Customer.
    // You can modify this code as needed.
    manageOrdersDataSetCustomerTableAdapter = new
            OrderManager.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)
{
    if (rowView != null)
    {
        ManageOrdersDataSet.CustomerRow row = rowView.Row
            as ManageOrdersDataSet.CustomerRow;
        if (row.IsNull("Name"))
        {
            row["Name"] = "Defailt";
        }
        if (row.IsNull("Address"))
        {
            row["Address"] = "Address Def";
        }
        if (row.IsNull("ID"))
        {
            row["ID"] = Guid.NewGuid();
        }
        this.manageOrdersDataSetCustomerTableAdapter.Update(row);
    }
}

Upvotes: 2

Views: 1207

Answers (1)

Yuki
Yuki

Reputation: 43

I found the solution. I cannot explain it well but apparently while event RowEditEnding the new data row is not considered to have values or changed. so I decided to call event handler.

Here are the steps

  1. Add event handler to Windows_Loaded:
manageOrdersDataSet.Customer.RowChanged +=
        new DataRowChangeEventHandler(Row_Changed);
manageOrdersDataSet.Customer.RowDeleted +=
        new DataRowChangeEventHandler(Row_Changed);
  1. Implement event handler:
public void Row_Changed(object sender, DataRowChangeEventArgs e)
{
    try
    {
        this.manageOrdersDataSetCustomerTableAdapter.Update(
            manageOrdersDataSet.Customer);
    }
    catch
    {
        // Do something...
    } 
}

Upvotes: 1

Related Questions