We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GridTemplateColumn not working using Code

Hi Guys,

GridTemplateColumn not working using Code, but works if I use XAML.
I get lot of missing rows for some reason. Below is my code. my model is same as the sample you guys provide online.
Thanks for the Help!!

 grid = new SfDataGrid();
            var data = new OrderInfoRepository();
            grid.AutoGenerateColumns = false;
            var label1 = new Label()
                TextColor = Color.Black,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center
            var label2 = new Label(){
                TextColor = Color.Black,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center

            var cellGrid = new Grid(){
                ColumnDefinitions = {
                    new ColumnDefinition{Width = GridLength.Auto},
                    new ColumnDefinition{Width = GridLength.Auto}
            cellGrid.Children.Add(label2, 1, 0);
            label1.SetBinding(Label.TextProperty, "CustomerID");
            label2.SetBinding(Label.TextProperty, "ShipCountry");

            var template = new GridTemplateColumn(){HeaderText = "Custom", MappingName = "OrderID" , Width = 250};
            var dataTemplate = new DataTemplate(() =>
                return cellGrid;
            template.CellTemplate = dataTemplate;
            grid.ColumnSizer = ColumnSizer.Auto;
            grid.ItemsSource = data.OrderInfoCollection;
            Content = grid;

Attachment: TestSyncFusion_9a21f5af.zip

3 Replies

DS Divakar Subramaniam Syncfusion Team January 24, 2017 10:10 AM UTC

Hi Vivek, 
Thanks for contacting Syncfusion Support. 
We have checked your query. We have resolved the query in sample level by creating new label inside a DataTemplate instead of creating outside of DataTemplate. Please refer the below code snippet. 
var dataTemplate = new DataTemplate(() => 
    var label1 = new Label() 
        TextColor = Color.Black, 
        HorizontalOptions = LayoutOptions.Center, 
        VerticalOptions = LayoutOptions.Center 
    label1.SetBinding(Label.TextProperty, "OrderID"); 
    return label1; 
We have attached the modified sample for your reference and you can download the same from the below link. 

KG K Guptha February 4, 2017 12:53 AM UTC

Hi, in this example xaml code GridTemplareColumn is not working for me, it showing only OrderID, not displaying other binding values.

Attachment: TestSyncFusion954146049_426fecc.zip

DS Divakar Subramaniam Syncfusion Team February 4, 2017 06:54 AM UTC

Hi Kumar, 
Thanks for the update. 
In the previous sample, we have added only one label and bound OrderID property to that label. That’s why only the OrderID values were displayed in SfDataGrid. Now, we have modified the sample based on your requirement and you can download the sample from the below link. 

Live Chat Icon For mobile
Up arrow icon