views:

78

answers:

1

I have an issue with a datagrid inserting/updating rows twice. The datagrid is bound to an observable collection that has an ItemEndEdit event added to it:

/// <summary>
/// extends the ObservableCollection class to include adding an ItemEndEdit event
/// </summary>
public class ObservableProjectExpenseItems : ObservableCollection<ProjectExpenseItemsBO>
{
    protected override void InsertItem(int index, ProjectExpenseItemsBO item)
    {
        base.InsertItem(index, item);
        item.ItemEndEdit += new ProjectExpenseItemsBO.ItemEndEditEventHandler((x) =>
        {
            if (ItemEndEdit != null)
                ItemEndEdit(x);
        });
    }

    public event ProjectExpenseItemsBO.ItemEndEditEventHandler ItemEndEdit;
}

ProjectExpenseItemsBO is my model/business object that contains the properties bound in the columns of the dg.

I use the following in my viewmodel to insert/update records when the user leaves the row of the datagrid:

    void ProjectExpenseItemsItemEndEdit(IEditableObject sender)
    {
        ProjectExpenseItemsBO projectExpenseItemsBO = sender as ProjectExpenseItemsBO;
        if (projectExpenseItemsBO.RowID == 0)
        {
            // if the ProjectExpenseItemsBO is a new row
            projectExpenseItemsRepository.AddProjectExpenseItem(projectExpenseItemsBO, this.ProjectExpenseID);
        }
        else
        {
            projectExpenseItemsRepository.UpdateProjectExpenseItem(projectExpenseItemsBO);
        }
        // get the total of the project expense items
        ItemTotal = projectExpenseItemsRepository.GetItemTotal(this.ProjectExpenseID);
    }

Both the update and insert fire twice and I cannot figure out why. I attach the ItemEndEdit in the initialization of the class/viewmodel:

ObservableProjectExpenseItems projectExpenseItemsCollection;
List<ProjectExpenseItemsBO> list = new List<ProjectExpenseItemsBO>(projectExpenseItemsRepository.GetProjectExpenseItems(this.ProjectExpenseID));
list.ForEach(item => ProjectExpenseItemsCollection.Add(item));
ProjectExpenseItemsCollection.ItemEndEdit += new ProjectExpenseItemsBO.ItemEndEditEventHandler(ProjectExpenseItemsItemEndEdit);

Finally, here is my datagrid:

<DataGrid ItemsSource="{Binding Path=ProjectExpenseItemsCollection}" Grid.Row="0" Grid.Column="0" AutoGenerateColumns="False" 
  Name="dgProjectExpenseItems" SelectionMode="Single" SelectionUnit="FullRow" CanUserResizeColumns="True" 
  SelectedItem="{Binding Path=SelectedProjectExpenseItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
  GridLinesVisibility="Horizontal" CanUserDeleteRows="True" CanUserAddRows="True">
  <DataGrid.RowValidationRules>
   <vr:RowDataInfoValidationRule ValidationStep="UpdatedValue" />
  </DataGrid.RowValidationRules>
  <DataGrid.Columns>
   <DataGridTextColumn Header="ID" Width="SizeToCells" IsReadOnly="True" MinWidth="50" Binding="{Binding RowID}" />
   <DataGridTextColumn Header="Project Expense ID" IsReadOnly="True" Width="SizeToCells" Visibility="Hidden" MinWidth="0" Binding="{Binding ProjectExpenseID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
   <DataGridTextColumn Header="Item Number" Width="SizeToCells" MinWidth="140" Binding="{Binding ItemNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
   <DataGridTextColumn Header="Item Description" Width="SizeToCells" MinWidth="250" Binding="{Binding ItemDescription, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
   <!--<DataGridTextColumn Header="Qty" Width="SizeToCells" MinWidth="65" Binding="{Binding ItemQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
   <DataGridTextColumn Header="Qty">
    <DataGridTextColumn.Binding>
     <Binding Path="ItemQty" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
       <vr:PositiveDecimalValidationRule ValidationStep="RawProposedValue" />
      </Binding.ValidationRules>
     </Binding>
    </DataGridTextColumn.Binding>
   </DataGridTextColumn>
   <DataGridTextColumn Header="Unit Price">
    <DataGridTextColumn.Binding>
     <Binding Path="ItemUnitPrice" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
       <vr:PositiveDecimalValidationRule ValidationStep="RawProposedValue" />
      </Binding.ValidationRules>
     </Binding>
    </DataGridTextColumn.Binding>
   </DataGridTextColumn>
   <!--<DataGridTextColumn Header="Unit Price" Width="SizeToCells" MinWidth="90" Binding="{Binding ItemUnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />-->
   <DataGridTextColumn Header="Supplier Name" Width="SizeToCells" MinWidth="200" Binding="{Binding SupplierName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
  </DataGrid.Columns>
 </DataGrid>
A: 

Alright. After trying to figure out a different issue I was having, I stumbled upon what was causing the event to fire twice. when I would save a row in the datagrid, I noticed the primary key field value was zero before and after the save. Obviously, this was due to me not refreshing the collection after the save method in my viewmodel was called. when I changed my code in the viewmodel to refresh the collection:

    void ProjectExpenseItemsItemEndEdit(IEditableObject sender)
    {
        ProjectExpenseItemsBO projectExpenseItemsBO = sender as ProjectExpenseItemsBO;
        if (projectExpenseItemsBO.RowID == 0)
        {
            // use the data access layer to update the wrapped data object
            projectExpenseItemsRepository.AddProjectExpenseItem(projectExpenseItemsBO, this.ProjectExpenseID);
        }
        else
        {
            projectExpenseItemsRepository.UpdateProjectExpenseItem(projectExpenseItemsBO);
        }

        //refresh the collection
        ProjectExpenseItemsCollection.Clear();
        List<ProjectExpenseItemsBO> list = new List<ProjectExpenseItemsBO>(projectExpenseItemsRepository.GetProjectExpenseItems(this.ProjectExpenseID));
        list.ForEach(item => ProjectExpenseItemsCollection.Add(item));

        // get the total of the project expense items
        ItemTotal = projectExpenseItemsRepository.GetItemTotal(this.ProjectExpenseID);

    }

the problem with duplicate records went away. Wow, I have been working on this for a couple weeks now. funny how something so basic can throw you for a loop for so long, especially when you are trying to learn something new (wpf, mvvm).

steveareeno