locked
delete row from DataGrid and EF table RRS feed

  • Question

  • Hi All.

    The UI layer has DataGrid one of the column has delete button. That is method that I try to delete DataGrid row

    private void DeleteEmployee_BTn_Click(object sender, RoutedEventArgs e)
    {
        if (!(Employee_DG.SelectedValue == null))
        {
            if (MessageBox.Show("Are you sure you want to delete", "Are you sure?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                int empid = (int) Employee_DG.SelectedValue;
                ProjectAdapter.DeleteEmployee(empid);
            }
        }
    }

    When I run application and hit Delete button in line
    int empid = (int) Employee_DG.SelectedValue;

    I got error message:
    System.InvalidCastExeptions: 'Specified cast is not valid.'

    When I'm gebugging step by step and check value in SelectedValue it displays rows values. How modify the code to retrieve only EmployeeId value?

    Thanks.




    • Edited by zleug Friday, August 28, 2020 2:53 PM
    Friday, August 28, 2020 1:52 PM

Answers

  • You could cast the row as employee and get the id as shown here.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by zleug Sunday, August 30, 2020 4:34 AM
    Friday, August 28, 2020 3:06 PM

All replies

  • You could cast the row as employee and get the id as shown here.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by zleug Sunday, August 30, 2020 4:34 AM
    Friday, August 28, 2020 3:06 PM
  • Thanks Karen
    Sunday, August 30, 2020 4:34 AM