Coding Geek

A blog by Nadeem Iqbal

SilverLight DataGrid Binding

 

I was working on the unbound SilverLight DataGrid and thought to write one in this regard,

There are scenarios when you don't have a predefined collection of objects to bind to SilverLight DataGrid or you don't know the structure of the object to bind with untill run time. For Example you want to bind the DataGrid with XML or to any other source.

So the way is to Create the Structure (Type/Class) of object dynamically on the fly and then build the generic List of newly created Type and bind it to SilverLight DataGrid. Moreover you want to add the new empty rows in grid, get the object bound to that row, Extract the values from the bound object, delete a row etc......

 Lets first look at how to create a new Type(class) on the fly with properties and bind it to the DataGrid;

Dynamic Typed Objects Creation by Vladimir Bodurov (thanks)

Update the DataSourceCreator class  to add a public property to maintain the type of the new structure

public static Type getType{get; set;}

and set this property before the last return statement of the ToDataSource function

getType = tb.CreateType(); 

Now how to get the bound Object of selected row of DataGrid on a button click & get values of the properties

var item = Activator.CreateInstance(DataSourceCreator.getType);
int index = mygrid.SelectedIndex;
           
item = mygrid.SelectedItems[0];

MethodInfo theMethod = DataSourceCreator.getType.GetMethod("get_Column1");
object res = theMethod.Invoke(item, new object[0]);  

in the above code i created the new var variable of the Type and stores the selected bound object in it. And then using reflection extracted the value of the property Column1.

Lets Add new row in SilverLight DataGrid

 IEnumerable<object> collection = mygrid.ItemsSource.Cast<object>();
List<object> list = collection.ToList();


var item = Activator.CreateInstance(DataSourceCreator.getType);
            
list.Add(item);

mygrid.ItemsSource = list;

It first gets the bound collection of the DataGrid and then adds a new object into the collection

Similarly you can remove an object from the bound collection

list.Remove(item)

 

Its all about Unbound Simulation of SilverLight DataGrid. Cheers :)

Comments

Iqbal said:

Interesting Article. We can do everything here which can be done using the Bound DataGrid

# October 6, 2008 5:19 AM

Inna said:

I have found very stupid bug in datagrid on silverlight 2.0 version.

steps:

1. give a list to a grid. for example 5 items.

2. remove some item from it list.Remove(item)

3. set another list to ItemsSource. also with 5 or more elements.

Result. You will see new list but with empty row in a plase of previously removed row.

How can I use this grid with such bug without recreating grid everytime I want to set another list? How can I refersh it, if seter metthod for ItemsSource must be doest makes cleaning work of rows from a privious list. I've tried to set null before.

For site developers: You are also writers of bugs)))

And why does copy/past doest work in this edit box where I'm writing this message? (ctrl+c) :-)))

# April 17, 2009 11:08 AM

jtoth55 said:

same issue here as inna, deleting a row causes a blank row to show up, very annoying...

# May 18, 2009 4:21 PM

niqbal said:

Here is the code to delete row from the datagrid. Actually after deleting the element from the collection you will have to rebind the new collection with the DataGrid.

if (mygrid.SelectedItem != null)

{

object objRow = mygrid.SelectedItem;

IEnumerable<object> collection = mygrid.ItemsSource.Cast<object>();

List<object> list = collection.ToList();

list.Remove(objRow);

mygrid.ItemsSource = list;

}

It will solve the issue

# July 6, 2009 1:35 AM

Jon said:

Vladimirs code was great, thanks for the pointer... however, I'm not sure what your code is referring to here:

MethodInfo theMethod = DataSourceCreator.getType.GetMethod("get_Column1");

What is get_Column1 supposed to do, and where do you place it?

# August 30, 2009 6:02 PM

Samrat said:

Nadeem,

       Thanks a lot for an excellent article. Did the job for me.

Jon,

If you have a field called 'Column1' then you should use 'get_Column1'

this line of code gives you the list of methods that are exposed

MethodInfo[] methods = DataSourceCreator.getType.GetMethods();

Hope this helps

# September 22, 2009 11:34 AM

Deepak said:

what is tb in

tb.CreateType();  

# November 21, 2011 5:04 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)