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?

    Regards,

    Henrique

  2. Answer
    Hristo Deshev
    Admin
    Hristo Deshev avatar
    559 posts
    Member since:
    Sep 2012

    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() 
        { 
            InitializeComponent(); 
     
            RadGridView1.ItemsSource = from index in Enumerable.Range(0, 100) 
                                           select new { Sender = index + "tom@hanna-barbera.com", 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