Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 978377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:02:57+00:00 2026-05-16T04:02:57+00:00

I have an issue with a datagrid inserting/updating rows twice. The datagrid is bound

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:02:58+00:00Added an answer on May 16, 2026 at 4:02 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.