Data Grid dynamic columns

Apr 28, 2009 at 5:31 PM

First of all I hope that I will make myself understood and then I hope that somebody can help me.

    I have a delicate situation where I had to create the columns for my data grid dynamically and as well bound them dynamically. The problem which occurred was that not all my objects which I have assigned to the source view of the data grid had that number of properties for columns to bind, so I was getting binding errors which were slowing down my application. To avoid that I used a converter for which I send as source the current object list of properties and as parameter the property Id. In this case the problem that I have is that if one of the properties is changing it will update the all list of properties (columns) for current object and I do not want that as I have to highlight only the current change. 

A solution that could work is to mach each object properties in the data source with the number of columns that I currently have in the grid, but that will again slow down the application as when something is changing I have to go through objects and update their properties.

If anybody could give me an idea on how to solve this it would be much appreciated.

 

Regards,

John.

Coordinator
Apr 28, 2009 at 8:54 PM
Edited Apr 28, 2009 at 8:56 PM
When properties are changing, do you mean that the value of a property on a row item changes, or do you mean that the actual properties that an object has changes?

If it is the former, this should not cause the columns list to update (unless you are doing that yourself).

If it is the later, then you would need to support some form of dynamically showing or hiding columns. You might also try using a converter on a binding that doesn't have a path (i.e. instead of {Binding Path=PropertyName} do {Binding Converter={StaticResource myConverter},ConverterParameter=PropertyName}. Then, use the converter parameter to specify the path. That way you may be able to find a way to support some objects with one set of properties and other objects with other properties.

Ben

Edited to show an example of binding with a converter.
Apr 29, 2009 at 9:17 AM

Hi Ben,

 

Thank you for your answer.

I did not mention, but because my dynamic data set, I did the columns set in the code behind. So, initially I had something like this:

 

DataGridTextColumn column = new DataGridTextColumn();

 

Binding path = new Binding(“PropertyList[”+property.Id+”].DisplayValue”); //because a property can have different values that can be displayed

 

path.NotifyOnTargetUpdated = true;

column.Binding = path;

 

So that was fine, except for binding errors when an object did not had the property with the id property.Id. To solve that I used a converter:

 

DataGridTextColumn column = new DataGridTextColumn();

Binding path = new Binding(“PropertyList”);

 

path.Converter = new PropertyIdToPropertyValueConverter();

path.ConverterParameter = property.Id;

 

path.NotifyOnTargetUpdated = true;

column.Binding = path;

 

In this case I loose the notification for DisplayValue if that’s chnages, because I have to send the notification for the all list of properties, so all propertyes DisplayValue will look like updating although they are not changing the value.

 

I tried your ideea of setting the path for the binding using the converter:

 

Binding path = new Binding();//or new Binding(“”); still does not work

path.Converter = new PropertyIdToPropertyValueConverter();

path.ConverterParameter = property.Id;

 

path.NotifyOnTargetUpdated = true;

column.Binding = path;

 

If I use the converter this way it will never be called, is like the converter requires a Path by default. Is something that I miss out or the solution that you proposed is for Xaml only?

 

Regards,

May 1, 2009 at 5:23 PM
In the end I fund the solution, which is not too complicated:

I needed a PropertyList class like this:

public

 

class PropertyList

 

{

 

private Dictionary<int, Property> _data = new Dictionary<int, Property>();

 

 

public Property this[int index]

 

{

 

get

 

{

 

if (_data.ContainsKey(index))

 

 

return _data[index];

 

 

return null;

 

}

 

set { _data[index] = value; }

 

}
}

This way I can keep my initial path Binding path = new Binding(“PropertyList[”+property.Id+”].DisplayValue”); , have not binding errors and as well have any DisplayValue update notification that my occure.

In case that somebody else may have a similar problem, please follow the next link for more information:

http://www.scottlogic.co.uk/blog/wpf/2009/04/binding-a-silverlight-datagrid-to-dynamic-data-part-2-editable-data-and-inotifypropertychanged/

Cheers.