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

Bind / Get key value from Anonymous Type (LINQ)

1 Answer 148 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Henrique Duarte
Top achievements
Rank 1
Veteran
Henrique Duarte asked on 09 Feb 2009, 04:39 PM
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

1 Answer, 1 is accepted

Sort by
0
Accepted
Hristo Deshev
Telerik team
answered on 11 Feb 2009, 10:41 AM
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.
Tags
GridView
Asked by
Henrique Duarte
Top achievements
Rank 1
Veteran
Answers by
Hristo Deshev
Telerik team
Share this question
or