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.
In my first comments I had assumed that the Objects are in fact contained in the
Cells
'Values
. In that case theCells
show whatever the object class'sToString()
methods returns. The name 'Value' is a little misleading here as it can hold any object and has nothing to do with value vs reference types.But in our chat we have established that you create a
DataTable
and fill it by Linq'ing the objects' properties into it as strings.So the
DataTable
as well as the boundDataGridView
contain only strings without any reference back to the objects.To solve the problem you have to include a reference to the original object instances somehow.
One way is to add a
Column
to theDataTable
to hold the reference, maybe like this:and fill it with the object references, either by including it in the Linq result set or by including it in the list of columns of your
Add()
command or by setting it separately.To hide the reference you can set the column in the DGV to be invisible..
..and to access the object you cast the
Value
to your object class:Another way to display object
Properties
as columns in aDataGridView
is to put them into aList<T>
, here aList<IECInstance >
.When you do that you can set the DGV's
DataSource
to this listIn such a solution the row is directly associated with the source object and you can refer to it on the row level like this:
You may want to insert one or two further levels of dataBinding by including
BindingList
and/or aBindingSource
thus adding functionality to the binding .