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

need to create a template column in code

4 Answers 82 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Teresa
Top achievements
Rank 2
Teresa asked on 06 Oct 2010, 01:38 AM

I am using the following code to dynamically load columns in a Silverlight datagrid and would prefer to use the Telerik Grid.  My first problem is trying to figure out what the equivalent of the DataGridTemplatolumn is with the GridView.
Can anyone help with how to dynamically create columns or help with converting the following code to the Telerik GridView?


private DataGridTemplateColumn CreateTemplateColumn(int i, string propNumber, string propName) 
       
           DataGridTemplateColumn column = new DataGridTemplateColumn(); 
           column.Header = String.Format("{1}", i, propName); 
           column.CellTemplate = (DataTemplate)XamlReader.Load(CreateColumnTemplate(i, "AllocationUnits")); 
          return column; 
       
private string CreateColumnTemplate(int index, string propertyName) 
       
           StringBuilder CellTemp = new StringBuilder(); 
           CellTemp.Append("<DataTemplate "); 
           CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' "); 
           CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>"); 
           CellTemp.Append(String.Format("<TextBlock Text='{{Binding ItemCustomers[{0}].{1}}}'/>", index, propertyName)); 
           CellTemp.Append("</DataTemplate>"); 
           return CellTemp.ToString(); 
       }

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 06 Oct 2010, 08:51 AM
Hello Teresa Burger,

You may use GridViewDataColumn for example and define its CellTemplate and CellEditTemplate. However, depending on your exact requirements, yo may use some of our specific column types demonstrated in our online documentation and demos
 

All the best,
Maya
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
Teresa
Top achievements
Rank 2
answered on 06 Oct 2010, 07:03 PM
I was able to do it without templates by using the following code.  Thank you!!
private static GridViewColumn CreateGridViewColumn (string fieldName,string title)
       {
           var column = new GridViewDataColumn();
           column.Header = title;
           column.DataMemberBinding = new System.Windows.Data.Binding(fieldName);
           return column;
       }
0
Michael Hilgers
Top achievements
Rank 1
answered on 21 Dec 2012, 10:56 AM
Just to resurect this thread:

I need to create a CellTemplate in code, because the binding is dynamic and i don't know the value at design time. So i do the following:

StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append(String.Format("<CheckBox IsChecked=\"{{Binding {0}, Mode=TwoWay}}\" telerik:StyleManager.Theme=\"Windows8\"/>", "Group_" + kvp.Key));
CellTemp.Append("</DataTemplate>");
 
col.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());

But i get an error which says that there is an "invalid character in qualified name. [at line1 position 15]" when assigning via 'col.CellTemplate = (DataTemplate)XamlReader.Load(CellTemp.ToString());'

Where is the error?

Kind Regards,
Michael
0
Teresa
Top achievements
Rank 2
answered on 21 Dec 2012, 07:34 PM
you need a > after data template and you may need a key and I am not sure why you double { your binding

here is what the final xaml should end up like

 

<DataTemplate x:Key="test">

 

 

<CheckBox IsChecked="{Binding test, Mode=TwoWay}" telerik:StyleManager.Theme="Windows8"/>

 

</DataTemplate>


Tags
GridView
Asked by
Teresa
Top achievements
Rank 2
Answers by
Maya
Telerik team
Teresa
Top achievements
Rank 2
Michael Hilgers
Top achievements
Rank 1
Share this question
or