Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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;
}


C#
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

1 solution

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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900