I have a System.Windows.Forms.DataGridView
to show the objects of my type IECInstance
.
I'm building a DataTable
and fill the table with my objects and set the DataTable as DataSource of my DataGridView. So my DataGridView is showing my objects correctly.
My problem is now to get the objects when i select the row(s).
My first attempt was to use this:
IECInstance theObjectIWant = dataGridView.SelectedRows[0].DataBoundItem as IECInstance.
But DataBountItem
returns a DataRowView
.
So i found many questions for the issue here on SO and some suggested to use this:
var drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
var row = drv.Row as DataRow;
var val = row[X] as MyType;
But as far i can see, row[X] is a access to the cell (column), so it does not match to my problem.
When i'm using a List<IECInstances>
as DataSource instead of the DataTable, the property DataBoundItem returns the proper object. But actually i don't want to set the DataSource as a List.
To make sure: When i talk about objects i mean my business object of the type IECInstace.