Highlighted
Absent Member.
Absent Member.
1718 views

casting an object from a wpf datagridrow

I have a wpf datagrid that and am trying to cast a datagrid.SelectedItem to a custom object. Getting the error "Unable to cast object of type 'System.Data.DataRowView' to type 'CarrierRecord'." when this line "set carrierRecord to DG_CellCarriers::SelectedItem as type CarrierRecord" is executed. Works just fine in c#.  Am I missing something basic here? My datagrid is bound to a datatable.

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: casting an object from a wpf datagridrow

The actual object that is bound to the dataGrid is a DataRowView. I can get at the selected data using something like the following using the Row property where ItemArray contains the columns in the row:

method-id dataGrid1_SelectionChanged.
procedure division using by value sender as object e as type System.Windows.Controls.SelectionChangedEventArgs.
declare selectedrow as type System.Data.DataRowView = self::dataGrid1::SelectedItem as type System.Data.DataRowView
declare mystring as string = selectedrow::Row::ItemArray[1]

end method.
0 Likes
Highlighted
Outstanding Contributor.
Outstanding Contributor.

RE: casting an object from a wpf datagridrow

This can be the solution to get an item of the DataGrid ItemsSource:
set carrierRecord to DG_CellCarriers::Items::CurrentItem as type CarrierRecord:

Consider, a new row is not of type CarrierRecord (CanUserAddRows="True")

Following 2 statements select same object in my program::
set GridZeile to DGWerte::Items::CurrentItem as type WPFApplDataGrid.GridZeile:
set GridZeile to DGWerte::SelectedItem as type WPFApplDataGrid.GridZeile:
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.