-
Hi guys, This is my cod in desing view:
and I tried this :
But unfortunately, I keep getting errors like this:
Can you help me with this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Can you provide a project we can run? Your code doesn't show what the data source is. |
Beta Was this translation helpful? Give feedback.
-
Okay so your data, as the error already suggests, comes from an anonymous type: lis.Items.Add(New With {Key .name = "Jack", Key .address = "3240 Vernon Street"})
lis.Items.Add(New With {Key .name = "Mary", Key .address = "3729 Cambridge Place"}) The lis.SelectedItem will contain the same objects you put in, not a "DataRowView". If you make up a type on the go as you did, it will be an anonymous type. You cannot access its members without Option Strict Off (or reflection). A better option is to declare your type ahead of time: Class MyRowData
Public Property name As String
Public Property address As String
End Class
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
lis.Items.Add(New MyRowData With {.name = "Jack", .address = "3240 Vernon Street"})
lis.Items.Add(New MyRowData With {.name = "Mary", .address = "3729 Cambridge Place"})
End Sub
Private Sub lis_PreviewMouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles lis.PreviewMouseDoubleClick
Dim str As String = CType(lis.SelectedItems(0), MyRowData).address
lbltest.Content = str
End Sub
End Class |
Beta Was this translation helpful? Give feedback.
Okay so your data, as the error already suggests, comes from an anonymous type:
The lis.SelectedItem will contain the same objects you put in, not a "DataRowView". If you make up a type on the go as you did, it will be an anonymous type. You cannot access its members without Option Strict Off (or reflection).
A better option is to declare your type ahead of time: