need to create a template column in code

5 posts, 0 answers
  1. Teresa
    Teresa avatar
    4 posts
    Member since:
    Aug 2012

    Posted 05 Oct 2010 Link to this post

    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(); 
           }
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 Oct 2010 Link to this post

    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
  3. DevCraft banner
  4. Teresa
    Teresa avatar
    4 posts
    Member since:
    Aug 2012

    Posted 06 Oct 2010 Link to this post

    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;
           }
  5. Michael Hilgers
    Michael Hilgers avatar
    76 posts
    Member since:
    Apr 2010

    Posted 21 Dec 2012 Link to this post

    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
  6. Teresa
    Teresa avatar
    4 posts
    Member since:
    Aug 2012

    Posted 21 Dec 2012 Link to this post

    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>


Back to Top
DevCraft banner