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

How-To Create CellTemplate in code

4 Answers 310 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard Harrigan
Top achievements
Rank 1
Richard Harrigan asked on 14 Jun 2011, 05:28 PM
Hi,

Can you show me how to execute the the following xaml or equivalent in code.  I need to execute this in the

RadGridView_DataLoading event.

Thanks
Rich



<telerik:GridViewColumn Header="">
                <telerik:GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <telerik:RadButton Click="Button_Click"
                                           Name="Expression"
                                           IsBackgroundVisible="False"
                                           ToolTipService.ToolTip="Add column"
                                           Margin="0">
                            <Image Source="Images/Expression_16.png"
                                   Height="16"
                                   Width="16"></Image>
                        </telerik:RadButton>
                    </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
            </telerik:GridViewColumn>

4 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 14 Jun 2011, 07:58 PM
Hello Richard,

 
In such case you may use different approaches.You may store the defined DataTemplate within UserControl's ResourceCollection and then reference it in code-behind as demonstrated below:

this.radGridView.Columns[0].CellTemplate=this.Resources["dataTemplate1"] as DataTemplate;

The second one is to set GridViewDataColumn's CellTemplate through calling XamlReader's Load method.
Just pass to this method the appropriate string defined which represents the desired DataTemplate.

Please let me know how this works for you!

Greetings,
Vanya Pavlova
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
Richard Harrigan
Top achievements
Rank 1
answered on 14 Jun 2011, 09:02 PM
Hi,

I used your first approach.  It worked great.

Thanks
Rich
0
gaurav
Top achievements
Rank 1
answered on 30 Jul 2019, 03:10 PM

Hi Admin,

I have a class that already inherits Windows class and it is not possible for me to inherit the UserControl class to implement the first method that you suggested. Is there a way around this?

Also, can you2a template by adding it to a string and then later adding it to one of the columns in the Grid. (I am generating columns in code behind file). Any help is appreciated.

 

Thanks, 

Gaurav

 

 

0
Martin Ivanov
Telerik team
answered on 31 Jul 2019, 09:14 AM
Hello Gaurav,

The first approach will work also with a control deriving from Window. You can define the DataTemplate in the Window.Resources tag (the Resources collection). You can see this approach, along with an alternative one where the template is defined as a string in Using DataTemplate in Code the article.

Regards,
Martin Ivanov
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Richard Harrigan
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Richard Harrigan
Top achievements
Rank 1
gaurav
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or