Grabbing data out of grid

Sep 13, 2010 at 5:44 AM

How can I pull data back out of a BindableDataGrid using the BindableDataGrid.SelectedItem property?

The library creates a new type called DataRowObject, however I am unable to instantiate this type of object.

I am attempting to do the following lines of code:

BindableDataGrid mybdb = new BindableDataGrid ();
mybdb.DataSource = ds;            
mybdb.DataMember = "Table";   
mybdb.Margin = new Thickness(24, 292, 12, 12);            
mybdb.Width = 488;            
mybdb.Height = 255;            

Then on a button click:

* DataRow row = (DataRow)mybdb.SelectedItem;

if(row["ID"].ToString() == something)



* = It's crashing here.


Sep 13, 2010 at 3:25 PM

Hi KenEucker, this is because in fact the DataRow is created in a runtime assembly (actually I called it DataRowObject as you can see in the code). so if you try to cast to a different data type it will obviously crash.

Unfortunately you cannot cast it to the real type directly, since it's built using reflection, so I'll need to figure out a way to retrieve this value, most likely overriding the SelectedItem property in the BindableDataGrid class.

When I have a chance I'll take a look and let you know. :-)


Sep 13, 2010 at 3:36 PM

Yes I saw the runtime assembly, and I attempted to recreate the same type using the same method with the format of the data I have in the table. Unfortunately you can't cast using a runtime type. 

What I tried:

public static Type myDataRowObject
      DataRow dr = new DataRow();
      dr["col1"] = null;
      dr["col2"] = null;
      dr["col3"] = null;
      dr["col4"] = null;
      dr["col5"] = null;
      dr["col6"] = null;
      dr["col7"] = null;
      dr["col8"] = null;

      Assembly tmp = dr.EmitAssembly();
      object c = tmp.CreateInstance("DataRowObject");
      return tmp.GetType("DataRowObject");

And then trying to cast with:

myDataRowObject = (myDataRowObject)mybdb.SelectedItem;

Crashes as well.

Thanks for responding so quickly and thanks for your consideration.

Sep 14, 2010 at 5:05 AM

I got it!


PropertyInfo pi = myDataGrid.SelectedItem.GetType().GetProperty("COL1");

object val = pi.GetValue(myDataGrid.SelectedItem, null);


I'll post some changes to the class later once I've got them finished, if you'd like.

Sep 14, 2010 at 2:50 PM

Yeah that should work great, thanks. :-)

Just sorry I can't dedicate more time to this project, I have "real life" projects to deal with, but I'll revisit some stuff later on (considering I already have 1600+ downloads of this project that started as an exercise!, that's so cool :-D).

In fact if you have time, you could work on writing a new dynamic mapper, overriding the SelectedItem property to actually return a DataRow type so we can retrieve it directly and avoid the end user the hassle of using reflection.


Sep 14, 2010 at 3:07 PM

That's the plan, and I will have that up soon. 

Jan 11, 2011 at 2:41 PM

Hi there, please check out the next release of the datagrid. It includes SelectedItem and SelectedItems properties thanks to your tip, as well as support for images inside the grid.

It also contains some minor fixes to the code.

Kind regards.