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
close icon

Finding Column ID from GridCell in DataTemplateSelector


Sorry for asking so many questions, but I'm trying to make sure this Grid will work for us before we spend the money to purchase it!

I've set up a DataTemplateSelector for my columns and when I receive the callback in my  protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) I can not figure out how to figure out the current column.
The first parameter is my bound object and  second parameter (container) is a GridCell, but the ColumnBase field is null.  Is there some other way I can get the column name?   

Code that creates the column and connects the selector

    GridTextColumn gtc = new GridTextColumn()
    {
        HeaderText = "Code Template",
        MappingName = "positionCode",
        CellTemplateSelector = templateSelector,
        SetCellBoundValue = true,                            
     };

Callback for template selection:
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)




1 Reply

JS Jayapradha S Syncfusion Team April 11, 2016 07:29 AM UTC

Hi Deanna,

Thank you for contacting Syncfusion Support.

We have analyzed your query.  You are getting the null item  in Container’s columnbase at first time in SelectTemplateCore since DataTemplateSelector is invoked before the content property has been initialized. You can get the column name from Container object in SelectTemplateCore when content has been initialized. Please refer the below code and sample,

Code Example:

public class CellTemplateSelectorExt:DataTemplateSelector

    {

        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)

        {

            if ((container as GridCell).ColumnBase != null)

            {

                Debug.WriteLine((container as GridCell).ColumnBase.GridColumn.MappingName);

                return App.Current.Resources["cellTemplate"] as DataTemplate;

            }

            return base.SelectTemplateCore(item, container);

        }
    }


 
Sample: http://www.syncfusion.com/downloads/support/forum/123673/ze/SfDataGrid_CellTemplateSelector1192384304

Regards,
Jayapradha


Loader.
Live Chat Icon For mobile
Up arrow icon