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

[Solved] Add a DataTemplate to a GridVeiwColumn in C#

2 Answers 372 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Adrian
Top achievements
Rank 1
Adrian asked on 11 Jan 2011, 04:54 PM
Hello,

I'm trying to add a DataTemplate to a cell. Works fine in XAML, but how can I do the same thing in code behind?
This is my XAML example:

<telerikGrid:RadGridView x:Name="gridView" Margin="12,40,12,0" AutoGenerateColumns="False" DataLoadMode="Asynchronous">           
  <telerikGrid:RadGridView.Columns>
    <telerikGrid:GridViewColumn Header="user" Width="130">
      <telerikGrid:GridViewColumn.CellTemplate>
        <DataTemplate>
        <StackPanel Width="auto" Height="25" VerticalAlignment="Center" Orientation="Horizontal">
            <TextBlock FontWeight="Bold" Text="{Binding UserName}" x:Name="nameColumn" />
          </StackPanel>
          </DataTemplate>
        </telerikGrid:GridViewColumn.CellTemplate>
    </telerikGrid:GridViewColumn>
  </telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>

I've got problems adding the Stackpanel to the DataTemplate.

Thanks for any help.
Adrian

2 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 11 Jan 2011, 05:07 PM
Hi Adrian,

The easiest way will be to define the DataTemplate in the Resources section of the UserControl for example and set it as a CellTemplate:

XAML:
<UserControl.Resources>    
    <DataTemplate x:Key="MyDataTemplate">
        <StackPanel Width="auto" Height="25" VerticalAlignment="Center" Orientation="Horizontal">
            <TextBlock FontWeight="Bold" Text="{Binding Name}"  />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>
 
C#:
    GridViewDataColumn column = new GridViewDataColumn();
    column.DataMemberBinding = new Binding("Name");
    column.CellTemplate = (DataTemplate)this.Resources["MyDataTemplate"];
 
    this.playersGrid.Columns.Add(column);
 

Best wishes,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Adrian
Top achievements
Rank 1
answered on 12 Jan 2011, 09:27 AM
Thank you Maya, this solves my problem. Great support.
Tags
GridView
Asked by
Adrian
Top achievements
Rank 1
Answers by
Maya
Telerik team
Adrian
Top achievements
Rank 1
Share this question
or