31
votes

I have a class like this:

public partial class AdressBokPerson
    {
        public long Session { get; set; }
        public string Förnamn { get; set; }
        public string Efternamn { get; set; }
        public string Mail { get; set; }
    }

Added to a list:

private readonly List<AdressBokPerson> _avp = new List<AdressBokPerson>();

With binding to a dataGridView like this:

dataGridView1.DataSource = _avp;

So far so good.

Here is my question:

How do I find out 'which' object is selected when a row in the dataGridView is selected. I need to retrieve the selected object AdressBokPerson somehow.

1

1 Answers

75
votes

You get the object by casting the DataBoundItem to the specified type:

AdressBokPerson currentObject = (AdressBokPerson)dataGridView1.CurrentRow.DataBoundItem;