This is a migrated thread and some comments may be shown as answers.

Column filtering with .NET's DynamicObject

1 Answer 159 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 14 Jul 2011, 04:53 PM
Greetings,

I've taken an interesting approach to binding in the RadGridView in order to meet some business requirements. Specifically, I'm extending my bound object from System.Dynamic.DynamicObject in order to dynamically "add" properties to the object at run time based on user selection. Here's a simplified version of my bound object:

public class GridItem : DynamicObject, INotifyPropertyChanged
{
    public GridItem(DateTime dateTime, decimal value)
    {
        _dateTime = dateTime;
        _value = value;
    }
 
    //NOTE: The DateTime property is private forcing the GridView to use the DynamicObject implementation...
    private readonly DateTime _dateTime;
    private DateTime DateTime
    {
        get { return _dateTime; }
    }
 
    //NOTE: The Value property is private forcing the GridView to use the DynamicObject implementation...
    private decimal _value;
    private decimal Value
    {
        get { return _value; }
        set
        {
            _value = value;
 
            OnPropertyChanged("Value");
        }
    }
 
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;
 
        switch (binder.Name)
        {
            case "DateTime":
                result = DateTime;
                break;
 
            case "Value":
                result = Value;
                break;
        }
 
        return true;
    }
 
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        switch (binder.Name)
        {
            case "Value":
 
                decimal result;
                if(decimal.TryParse(value.ToString(), out result))
                {
                    Value = result;
                }
 
                break;
        }
 
        return true;
    }
 
    private static readonly IEnumerable<string> _members =
         new List<string>() { "DateTime", "Value" };
 
    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return _members;
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged == null)
            return;
 
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

I add the items to my GridView using:

this.GridView.ItemsSource =
     new List<GridItem>()
          {
               new GridItem(DateTime.Parse("1/1/2009"), 10.9m),
               new GridItem(DateTime.Parse("3/31/2010"), 11.4m),
               new GridItem(DateTime.Parse("5/1/2011"), 12.5m),
          };

And my Grid is defined as such:

<Telerik:RadGridView x:Name="GridView" AutoGenerateColumns="False">
     <Telerik:RadGridView.Columns>
          <Telerik:GridViewDataColumn UniqueName="DateTime" Header="Date/Time"
               DataMemberBinding="{Binding DateTime}" IsReadOnly="True"
               DataType="{x:Type System:DateTime}" IsFilterable="True" />
          <Telerik:GridViewDataColumn UniqueName="Value" Header="Value"
               DataMemberBinding="{Binding Value}" DataType="{x:Type System:Decimal}"
               IsFilterable="True" />
     </Telerik:RadGridView.Columns>
</Telerik:RadGridView>

The approach works great for binding; however, the column filtering does not work. It seems to be broken when there's not a public property exposed.

I'm using the Q3/2010 Telerik controls.

Any help or work around would be appreciated. We must have column filtering.

Thanks a bunch.

1 Answer, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 15 Jul 2011, 07:41 AM
Hi John,

Can you try with the Q2 2011 version that was released two days ago? It should be working there. Let us know if there are problems.

Kind regards,
Ross
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or