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

Why is binding a column to a property in an an anonymous type as hard as it is?

2 Answers 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stuart Hemming
Top achievements
Rank 2
Stuart Hemming asked on 24 Jun 2010, 02:24 PM
OK, I prepared to accept that I'm doing this wrong but...

I have a grid that includes the following column definition ...
<telerik:GridBoundColumn  
  HeaderText="Name"  
  SortExpression="Name"  
  UniqueName="Name"
</telerik:GridBoundColumn> 

My code behind reads something like this ...

var l = from contact in contacts 
        select new 
        { 
           ContactID = contact.ContactID, 
           Name = contact.LastName + ", " + contact.FirstName, 
        }; 
gdContacts.MasterTableView.DataSource = l; 

When I run this, rather than a value of "Hemming, Stuart" the "Name" column contains a ".ToString()"-like value that reads "{ContactID=1, Name="Hemming, Stuart"}.

Things work as expected if I change my column definition to a template column like this...
<telerik:GridTemplateColumn 
  HeaderText="Name" 
  SortExpression="Name"  
  UniqueName="Name"
  <ItemTemplate> 
    <asp:Label runat="server" ID="lblName" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>' /> 
  </ItemTemplate>                     
</telerik:GridTemplateColumn> 

So, why is that?

Enquiring minds, and all that.

-- 
Stuart

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 24 Jun 2010, 02:56 PM
Stuart,

You are missing the DataField property from the GridBoundColumn declaration.

All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Stuart Hemming
Top achievements
Rank 2
answered on 24 Jun 2010, 03:02 PM
[FX: Peers at code. Does a double-take and slaps head] Doh!

And you have no idea how long I looked at that code.

Sometimes, I shouldn't even be allowed out!

Thanks Rosen.

-- 
Stuart, aka Clueless of Grantham.
Tags
Grid
Asked by
Stuart Hemming
Top achievements
Rank 2
Answers by
Rosen
Telerik team
Stuart Hemming
Top achievements
Rank 2
Share this question
or