Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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.");
        else
        {
                OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();
 

                update.ShowDialog();
                Page_Loaded(null, null);
        }
    }
 
Any help would be much appreciated.
 
Many thanks, Greg.
Posted 16-Nov-12 0:43am

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
gregsagan at 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
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 220
2 Peter Leow 205
3 _Amy 190
4 Andreas Gieriet 180
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,412
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,702


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
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