Freelance Project Requests Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net

Solved: System.InvalidCastException   HResult=0x80004002



Error message

System.InvalidCastException  HResult=0x80004002

  Message=Das Objekt des Typs "<>f__AnonymousType6`6



kann nicht in Typ "Schichtplaner.tblBase_Mitarbeiter" umgewandelt werden.


What does the error message mean:

The error message indicates that an array of integers and strings cannot be converted into a table structure.



The error occurs aFor example, if you select a line in a WPF DataGrid. The data close has been tied to a table via binding.



Alter Code, C#, XAML zugriff auf WPF DataGrid

private void CtlDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)


    if (ctlDataGrid.SelectedItem != null)


        var item = (tblBase_Mitarbeiter) ctlDataGrid.SelectedItem;







If you tie a line from multiple tables about join queries in a data grid, then the selection of a single line cannot be converted into the format of a table.

As a solution, you can evaluate the SelectedValue of a grid. To do this, however, a column must be set via the SelectedValuePath.


New code with query of a DataGrid's SelectedValue


private void CtlDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)


    int ID = (int) ctlDataGrid.SelectedValue;





In the xaml file, the SelectedValue must be set to a column of the data grid

<DataGrid x:Name="ctlDataGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"














            <DataGrid.Columns >

                <DataGridTextColumn Header="ID" Binding="{Binding IDMitarbeiter}"/>

                <DataGridTextColumn Header="Nachname" Binding="{Binding Nachname}"/>

                <DataGridTextColumn Header="Vorname" Binding="{Binding Vorname}"/>

                <DataGridTextColumn Header="Schicht" Binding="{Binding Schicht}" />

                <DataGridTextColumn Header="Maschine" Binding="{Binding Maschine}"/>

                <DataGridTextColumn Header="MA Gruppe" Binding="{Binding Mitarbeitergruppe}" Width="*"/>






If a line is selected now, the result is not the line but the value of the defined column.






Full details error message




  Message=Das Objekt des Typs "<>f__AnonymousType6`6[System.Int32,System.String,System.String,System.String,System.String,System.String]" kann nicht in Typ "Schichtplaner.tblBase_Mitarbeiter" umgewandelt werden.




   at Schichtplaner.Page_Base_Mitarbeiter.CtlDataGrid_SelectionChanged(Object sender, SelectionChangedEventArgs e)





WPF Datagrid: Set Color of Row when it has no Focus
WPF Datagrid: Change the Color of Selected Row
WPF: Change Selected Row Color in DataGrid
WPF: Read SelectedItem from DataGrid with Dynamic As Anonymous Type
WPF: Elements too wide, edge Cut off
Solved: System.InvalidCastException HResult 0x80004002 in Select DataGrid
WPF: Combobox Selected Value
WPF Datagrid: Column is not displayed
WPF: Show video in preview
WPF Webbrowser: Suppress Java Errors or Set WebBrowser to Silent


Contact for Jobs, Project Requests: