Reputation: 2485
First off, I found a similar question here, but don't understand how it works, and I'm not comfortable 'bumping' or replying to a couple month old question.
I have a datagridview control and it's DataSource property is set to a List type object.
Question: I'm trying to figure out how to reference the actual object of a selected row so I can display extra data about that object.
I've got the SelectionChanged event hooked up from the DataGrid View. The object is a reference to the row/computer selected on the DGV.
Private Sub LabUsersList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxLabUsersList.SelectionChanged
Dim selectedComputer = Me.uxLabUsersList.SelectedRows(0)
End Sub
Update: I'm seeking the underlying object from the list element, not the list element (DataSource).
Thanks in advance for the help :)
Upvotes: 3
Views: 6728
Reputation: 3993
Once you have the collection of SelectedRows (each element in the collection is a DataGridViewRow obect), you can get the underlying data bound item using the DataBoundItem property for each row.
In your case, try the following:
Private Sub LabUsersList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxLabUsersList.SelectionChanged
Dim selectedComputer = Me.uxLabUsersList.SelectedRows(0).DataBoundItem
End Sub
Upvotes: 7