Search code examples
c#objectlistview

How to get data from a specific column of selected item(s) in ObjectListView?


I have an ObjectListView and I need to get the value of a specific column of the row that has been clicked at.

For example:

Imgur (I would put the image here directly, but I don't have the reputation to do so.)

I need to get the Id from the row that I clicked on (doesn't matter where on the row I clicked - I mean on what column within the row).

Thanks.


Solution

  • Use the SelectionChanged event and access the corresponding property of your model object from there. Assuming you model class is named MyDataObject:

        private void olv_SelectionChanged(object sender, EventArgs e) {
            if (olv.SelectedObject is MyDataObject data) {
                // Access data.Id or whatever else you need
            } 
        }