How do I create an object from a row in a DataGridView / BindingSource?

I would like to create an Object (say Client) from the currently selected row in a DataGridView. The DataGridView's DataSource is a BindingSource, for which the DataSource is a DataSet containing a single Table of Clients. I have been advised to use the BindingSource.Current property to get the selected item (or row), rather than the DataGridView. I have tried the following:

DomainClient c_client = (DomainClient)c_bindingSource.Current;

which results in an exception.

How can I get around this

Thanks in advance,

JackStri.



Answer this question

How do I create an object from a row in a DataGridView / BindingSource?

  • Lucia_fernadez

    Hi,

    I guess I replied more than year after you posted this.

    I was facing the same problem, until I found this solution

    DataRowView rowView = (DataRowView)empBindingSource.Current;

    ePCDataSet.empRow row = (ePCDataSet.empRow)rowView.Row;

    Regards,

    Andi



  • StephenJr

    Aaaaarrrgghh! I expected as much, but was hoping to get around it with less coding! Txs anyway.

    JackStri.


  • AndrasEliassen

    Remember that BindingSource, in your case, is binded to a DataSet. Therefore the current object would be a DataRow in the data set. You'll therefore need to conver the data row to your custom object using some method that you've written (maybe an overloaded constructor on the custom class or a static method).

    DataRow row = (DataRow)c_bindingSource.Current;
    DomainClient client = new DomainClient(row);

    Michael Taylor - 11/2/06


  • How do I create an object from a row in a DataGridView / BindingSource?