50

I need to get the currently selected object from da databound DataGridView.

I do not need the object of the current selected cell, but the object on which the whole row is based, in this case a BusinessObject whos properties make the columns of the grid.

I could go over the DataSource, but that itself is just an object and can be a BindingSource or a IBindingList or something like that - so not easy standartized way to get the wanted object.

Behind that is the need to just check the businessObject for a property called IsChanged and ask the user to save or discard the changes, before the bindingsource selects the next item. Therefore I must find out the current object inside RowValidating-Event of the DataGridView, since the BindingSource does not offer an event to stop changing before change occurs.See here for the well known problem

Thanks for reading ;-)

6 Answers 6

97

DataGridViewRow.DataBoundItem contains the 'business' object it is bound to.

4
  • 4
    Thank you for hinting me correctly, sometimes one should just lay back and relax instead of searching more intensly... Jan 18, 2010 at 9:55
  • 1
    This is much better than caching the data and then trying to use the selected index. Thank you!
    – styfle
    Oct 22, 2013 at 23:29
  • Just want to mention, that this also works, if your dataGrid is not 'bound' to the data, but instead if you just have applied it by 'DataSource'
    – oo_dev
    Oct 1, 2015 at 21:16
  • See this for a Example learn.microsoft.com/en-us/dotnet/api/…
    – nogood
    Jan 18, 2021 at 9:59
7

Here is my code put this into your Person class

 public static explicit operator Person(DataRow dr)
    {
        Person p = new Person();
        p.adi = dr.ItemArray[0].ToString();
        p.id = Int32.Parse(dr.ItemArray[1].ToString());
        p.soyadi = dr.ItemArray[2].ToString();
        p.kartNo = dr.ItemArray[3].ToString();
        p.dogumTarihi = DateTime.Parse( dr.ItemArray[4].ToString() );
        p.adres = dr.ItemArray[5].ToString();
        p.meslek = dr.ItemArray[6].ToString();
        p.telefon = dr.ItemArray[7].ToString();
        p.gsm = dr.ItemArray[8].ToString();
        p.eposta = dr.ItemArray[9].ToString();

        return p;
    }

and this is a update button click

DataRow row = (dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row;
Person selected = (Person)row;
4

You can also use this short code.

Person selected = dataGridView1.SelectedRows[0].DataBoundItem as Person;
0

What about this way?

foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
  MessageBox.Show(item.Cells[0].Value.ToString());
}

We can get multiple selected rows data.

0

Since you did state the IBindingList - yes as others have said the DataBoundItem property will give you what you need - there is an issue with it that I had experienced previously and ended up with a null reference but right now I can not think of the scenario in which it happened.

If you are databound using a BindingSource - you can capture the CurrentChanged, CurrentItemChanged events of your BindingSource , then you need not have an additional IsChanged Property on your B.O. .. , also the underlying datasource could also indicate modified - for example if you have BindingSource bound to a datatable the row would give you a modified flag.

-1

You can get the selected cell value like this

yourDGV.CurrentCell.Value;

If you want the value in the form of a String just use ToString() method like this

yourDGV.CurrentCell.Value.ToString();

This should do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.