Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Followers 0
Login Register as User

Solved: System.InvalidCastException HResult=0x80004002

29.12.2018 (👁3160)

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)