C1 Community
ComponentOne Community is a free source for developers and help authors to collaborate and communicate.

define DataGrid template column through code

rated by 0 users
This post has 3 Replies | 2 Followers

Not Ranked
Posts 1
csys_dc Posted: Mon, Jun 29 2009 2:20 PM

Hi,

I have a working Template Column (TwoLevelHeaderColumn) that displays three columns within a column with a title header.

|                TITLE                | Column4 | Column5 |
|Column1 Column2 Column3|               |               |

In the xaml page I reference this column control like this without a problem:

<C1_Silverlight_DataGrid:C1DataGrid x:Name="myGrid" Grid.ColumnSpan="1" Canvas.Left="0" Canvas.Top="25" Width="600" Height="645" Grid.Row="1" RowHeaderWidth="0"  CanUserEditRows="False" CanUserAddRows="False" ColumnHeaderHeight="30" AutoGeneratingColumn="myGrid_AutoGeneratingColumn" >
 <C1_Silverlight_DataGrid:C1DataGrid.Columns>
  <C1_Silverlight_DataGrid:DataGridTemplateColumn DisplayIndex="4">
   <C1_Silverlight_DataGrid:DataGridTemplateColumn.Header>
    <local:TwoLevelHeaderColumn ControlMode="Header"/>
   </C1_Silverlight_DataGrid:DataGridTemplateColumn.Header>
   <C1_Silverlight_DataGrid:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
     <local:TwoLevelHeaderColumn ControlMode="Cell"/>
    </DataTemplate>
   </C1_Silverlight_DataGrid:DataGridTemplateColumn.CellTemplate>
   <C1_Silverlight_DataGrid:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
     <local:TwoLevelHeaderColumn ControlMode="EditingCell"/>
    </DataTemplate>
   </C1_Silverlight_DataGrid:DataGridTemplateColumn.CellEditingTemplate>
  </C1_Silverlight_DataGrid:DataGridTemplateColumn>
 </C1_Silverlight_DataGrid:C1DataGrid.Columns>
</C1_Silverlight_DataGrid:C1DataGrid>

The rest of the columns are added dinamically depending on the collection used.
Now, I need to create and add this Template Columns to the grid through C# code but got stuck on the process.
Lets suppose we have:

TwoLevelHeaderColumn tlhc = new TwoLevelHeaderColumn();
DataGridTemplateColumn dgtc = new DataGridTemplateColumn();

and a Dependency Property "ControlModeProperty" that is based on the following type (enum):
  MergedContentType.Header
  MergedContentType.Cell
  MergedContentType.CellEditing
           
How can I configure the column to work the same way as in the xaml but through c# to then use myGrid.Columns.Add(dgtc); to add the columns ? The idea is to add more than once this template to the grid.
I'm new to silverlight and C1, and couldn't figure out how to solve this.

Thanks in advance

 

Top 25 Contributor
Posts 346

Hi,

Here is a sample of adding a template column from code,

var templateColumn = new DataGridTemplateColumn();
var cellTemplate = (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:local=""clr-namespace:C1DataGrid_CustomColumns;assembly=C1DataGrid_CustomColumns"" ><local:MergedColumnEditor ControlMode=""Cell"" /></DataTemplate>");
var cellEditingTemplate = (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" xmlns:local=""clr-namespace:C1DataGrid_CustomColumns;assembly=C1DataGrid_CustomColumns"" ><local:MergedColumnEditor ControlMode=""EditingCell"" /></DataTemplate>");
templateColumn.CellTemplate = cellTemplate;
templateColumn.CellEditingTemplate = cellEditingTemplate;
templateColumn.Header =
new MergedColumnEditor() { ControlMode = MergedContentType.Header };
grid.Columns.Add(templateColumn);

Anyway I believe in this case would be easier to create a custom column instead of using Template column.

Note I hard-coded the data template in the code, but you could put the data template as a resource in the page and take it from there.

Regards,

Alvaro.

Not Ranked
Posts 3
datblc replied on Mon, Oct 19 2009 4:40 AM

Hi Alvaro,

What does Namespace contain class MergedColumnEditor() ?

Thanks so much.

Top 25 Contributor
Posts 346

Hi datblc,

It is in the C1DataGrid_Demo sample, but now is called CompositeColumn.

Regards,

Alvaro.

Page 1 of 1 (4 items) | RSS
Contact ComponentOne: 1.800.858.2739 ©1987-2010 ComponentOne LLC All Rights Reserved.