Click here to Skip to main content
13,141,079 members (49,757 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am creating an application to get me started with WPF and using entity frameworks to allow CRUD operations.

So far I am able to add and delete items from a data-grid which loads the contents from a database.

But I'm having problems with my update method.

In the data-grid, when i select a row, and update it, it updates the first row of the table, not the row i selected (third row down for example).

I've posted the code snippets of what I think are important.

private void Page_Loaded(object sender, RoutedEventArgs e) 
        DBEntities context = new DBEntities();
        var orgTypeDetails = (from o in context.OrganisationTypeDetails
                              select o).ToList();
        dgOrgTypeDetail.ItemsSource = orgTypeDetails;

private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e) 
    OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;
        if (selected == null)
            MessageBox.Show("You must select a 'Type' before updating.");
                OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();

                Page_Loaded(null, null);

Any help would be much appreciated.

Many thanks, Greg.
Posted 16-Nov-12 0:43am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It should work. What I might suggest is to handle the SelectionChanged event. Then you can update a textbox with a value when it changes, and see what is happening. Might be loosing the selected item when you click on the text box.
gregsagan 22-Nov-12 5:20am
Hi Clifford Nelson, The problem seems to be that in the UpdateOrganisation method, it doesnt seem to get the selected OrganisationTypeDetail ID, therefore throwing the Exception. In my properties, if i assign it to int ID = 2, it updates the row from row (as its ID is 2).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100