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

Is it possible to reuse a grid columns definitions?

Hi,
Maybe this is a silly question, but I have the following scenario.
I have some Hierarchical classes where I have three main collections of Class1, Class2 and Class3, Class 1 has a collection of Class2 inside and Class3 has a collection of Class2 and a Collection of Class1 inside.
In my interface I have three main grids for the collections of Class1, Class2 and Class3 objects, and the Class1 and Class3 grids have a detail grid for the collections contained inside.
The question is, since I have 2 identical grids representing Class2 objects and 2 identical grids representing class 1 objects, is there a way for me to create the Sfdatagrid.columns definition for each object just once and reuse them on grids and detail grids representing the same object? this will allow me to work a little bit less because I have to make the column modifications just once instead of multiple times for each grid definition when changing things during development.
thank you in advance
Sabrina

1 Reply

FP Farjana Parveen Ayubb Syncfusion Team April 25, 2016 12:10 PM UTC

Hi  Sabrina, 
 
Thank you for contacting Syncfusion support. 
 
You can achieve your requirement ” Is it possible to reuse a grid columns definitions?” by defining the columns collection in ViewModel  and binding to Columns property in SfDataGrid. 
 
Please refer the code example and sample in the following location, 
 
Code Example (XAML): 
 
      <syncfusion:SfDataGrid  x:Name="dataGrid1" 
                        AutoGenerateColumns="True"  
                        ItemsSource="{Binding VM1}"> 
            <syncfusion:SfDataGrid.DetailsViewDefinition> 
                <syncfusion:GridViewDefinition RelationalColumn="Class1Value1"> 
                    <syncfusion:GridViewDefinition.DataGrid> 
                        <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid" AutoGenerateColumns="False" 
                         Columns="{Binding Columns, Source={StaticResource viewmodel}, UpdateSourceTrigger=PropertyChanged}" > 
                        </syncfusion:SfDataGrid> 
                    </syncfusion:GridViewDefinition.DataGrid> 
                </syncfusion:GridViewDefinition> 
            </syncfusion:SfDataGrid.DetailsViewDefinition> 
        </syncfusion:SfDataGrid> 
        <syncfusion:SfDataGrid  x:Name="dataGrid2" 
                                Grid.Column="1" 
                                AutoGenerateColumns="False"  
                                Columns="{Binding Columns, Source={StaticResource viewmodel}, UpdateSourceTrigger=PropertyChanged}" 
                                ItemsSource="{Binding VM2}" /> 
        <syncfusion:SfDataGrid  x:Name="dataGrid3" 
                                Grid.Column="2" 
                        AutoGenerateColumns="True"  
                        ItemsSource="{Binding VM3}"> 
            <syncfusion:SfDataGrid.DetailsViewDefinition> 
                <syncfusion:GridViewDefinition RelationalColumn="Class2Value3"> 
                    <syncfusion:GridViewDefinition.DataGrid> 
                        <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid1" AutoGenerateColumns="False" 
                         Columns="{Binding Columns, Source={StaticResource viewmodel}, UpdateSourceTrigger=PropertyChanged}"> 
                        </syncfusion:SfDataGrid> 
                    </syncfusion:GridViewDefinition.DataGrid> 
                </syncfusion:GridViewDefinition> 
                <syncfusion:GridViewDefinition RelationalColumn="Class1Value3"> 
                    <syncfusion:GridViewDefinition.DataGrid> 
                        <syncfusion:SfDataGrid x:Name="FirstDetailsViewGrid2" AutoGenerateColumns="True"> 
                        </syncfusion:SfDataGrid> 
                    </syncfusion:GridViewDefinition.DataGrid> 
                </syncfusion:GridViewDefinition> 
            </syncfusion:SfDataGrid.DetailsViewDefinition> 
        </syncfusion:SfDataGrid> 
 
Code Example (C# - ViewModel): 
 
    private Columns columns; 
        public Columns Columns 
        { 
            get 
            { 
                if (columns == null) 
                    columns = GetColumns(); 
                return columns; 
            } 
            set 
            { 
                this.columns = value; 
                OnPropertyChanged("Columns"); 
            } 
        } 
 
        protected Columns GetColumns() 
        { 
            Columns _columns = new Columns(); 
 
            GridNumericColumn ProductId; 
            ProductId = new GridNumericColumn() 
            { 
                MappingName = "ProductId", 
                 
            }; 
            _columns.Add(ProductId); 
 
            GridTextColumn ProductName2; 
            ProductName2 = new GridTextColumn() 
            { 
                MappingName = "ProductName2", 
 
            }; 
            _columns.Add(ProductName2); 
 
            GridNumericColumn SalesID2; 
            SalesID2 = new GridNumericColumn() 
            { 
                MappingName = "SalesID2", 
 
            }; 
            _columns.Add(SalesID2); 
 
 
            return _columns; 
        } 
 
Sample Location: DetailsViewSample
 
 
Regards, 
Farjana Parveen A 


Loader.
Up arrow icon