Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving selected row in dataGridView as an object

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.

like image 476
Dimo Avatar asked Oct 30 '13 12:10

Dimo


1 Answers

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

AdressBokPerson currentObject = (AdressBokPerson)dataGridView1.CurrentRow.DataBoundItem; 
like image 60
Rob Avatar answered Sep 29 '22 07:09

Rob