Bind / Get key value from Anonymous Type (LINQ)

2 posts, 1 answers
  1. Henrique Duarte
    Henrique Duarte avatar
    104 posts
    Member since:
    Oct 2003

    Posted 09 Feb 2009 Link to this post

    Hello guys,

    I have two questions:
    1. How can I bind a gridview from an Anonymous Type returned LINQ query?
    2. How can I get the key value from a grid bound from an Anonymous Type? On ASP.Net Ajax we use GetDataKeyValue method. Do you have a similar on WPF?


  2. Answer
    Hristo Deshev
    Hristo Deshev avatar
    559 posts

    Posted 11 Feb 2009 Link to this post

    Hello Henrique Duarte,

    Anonymous types are CLR types and RadGridView binds to them like any other type. It will detect their properties and autogenerate columns.

    RadGridView for WPF does not have the abstraction of data keys yet, and does not get values for the corresponding keys. You can use the API in the System.ComponentModel namespace to get the property values from the anonymous types. See the button Click event handler below for an example:

    public partial class Window1 : Window 
        public Window1() 
            RadGridView1.ItemsSource = from index in Enumerable.Range(0, 100) 
                                           select new { Sender = index + "", Subject = "Cats are cool", Size = 100 }; 
        private void getValueButton_Click(object sender, RoutedEventArgs e) 
            var firstDataRecord = (DataRecord) RadGridView1.CurrentRecord; 
            var dataItem = firstDataRecord.Data; 
            var senderValue = (string) TypeDescriptor.GetProperties(dataItem)["Sender"].GetValue(dataItem); 
            MessageBox.Show(senderValue, "Sender"); 

    I am attaching the project for your reference.

    Kind regards,
    Hristo Deshev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top