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

Count
Followers 0
Login Register as User

Solved: System.InvalidCastException HResult=0x80004002

29.12.2018 (👁10179)

Solved: System.InvalidCastException   HResult=0x80004002

 

 

Error message

System.InvalidCastException  HResult=0x80004002

  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.

 

What does the error message mean:

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

 

Occurrence:

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;

        load_Dataset_byID(item.IDMitarbeiter);

    }

}

 

 

Solution:

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

.cs

private void CtlDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

    int ID = (int) ctlDataGrid.SelectedValue;

    load_Dataset_byID(ID);

}

 

.xaml

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

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

          AutoGenerateColumns="False"

          GridLinesVisibility="All"

          Background="#F0f0f0"

          ItemsSource="{Binding}" 

          SelectionMode="Single" 

          IsReadOnly="True" 

          MouseDoubleClick="ctlDataGrid_MouseDoubleClick"

           SelectionChanged="CtlDataGrid_SelectionChanged"

           HeadersVisibility="All"                  

           Grid.Row="1"    

           SelectedValuePath="IDMitarbeiter"

          >

 

            <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="*"/>

                

            </DataGrid.Columns>

        </DataGrid>

 

Result:

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

 

SelectedValuePath=IDMitarbeiter

SelectedValue=2

 

 

Full details error message

 

System.InvalidCastException

  HResult=0x80004002

  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.

 

  Source=Schichtplaner

  StackTrace:

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