Click here to Skip to main content
15,887,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Any Help Me? I Want to load the Column in DataGrid Dymanically As per user preference. The user may be to mention the column order in the Text file. Then According to that, we have shown the column in the data grid.

My data grid has some control on a column like dropdown, text box, Checkbox,

I have added my sample code it is working fine, But in my requirement, I have to load the data grid column as per user preference (Not hardcoded like i am using currently).we can use the text file to configure the column, data grid header should display according to configure column in the text file.

HTML
<datagrid
 grid.row="1" 
="" grid.column="0" grid.columnspan="3" margin="0,4,0,0" padding="0" horizontalalignment="Stretch" verticalalignment="Stretch" autogeneratecolumns="False" canuseraddrows="False" cellstyle="{StaticResource MetroDataGridCell}" columnheaderstyle="{StaticResource DefaultDataGridColumnHeader}" itemssource="{Binding SelectedContact.Recipients, Mode=TwoWay}" rowheaderstyle="{StaticResource MetroDataGridRowHeader}" rowstyle="{StaticResource MetroDataGridRow}" scrollviewer.cancontentscroll="false" scrollviewer.horizontalscrollbarvisibility="Auto" scrollviewer.verticalscrollbarvisibility="Auto">
  <datagrid.columns>
  <datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.IsDeleted}">
     <datagridtemplatecolumn.celltemplate>
       <datatemplate>
         <checkbox horizontalalignment="Center" ischecked="{Binding IsDeleted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
       
     
  
<datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.WhatToSend}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <combobox
 itemssource="{Binding DataContext.WhatToSendTypes, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}" 
="" selectedvalue="{Binding WhatToSendInformationKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" selectedvaluepath="Identifier">
                                                            
                                                        
                                                    
<datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.OnlyIfSender}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <checkbox horizontalalignment="Center" ischecked="{Binding IsApplicableOnlyIfContactSender, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                                            
                                                        
                                                    
<datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.HowToSend}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <combobox
 itemssource="{Binding DataContext.HowToSendTypes, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}" 
="" selectedindex="0" selectedvalue="{Binding HowToSendInformationKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" selectedvaluepath="Identifier">
                                                            
                                                        
                                                    
<datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.ClientCode}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <textbox isreadonly="True" text="{Binding Path=ClientCode, Mode=TwoWay}">
                                                            
                                                        
                                                    
DataGridTemplateColumn Header="{x:Static infrastructureLayer:Translations.ForAnotherClient}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <textbox isreadonly="True" text="{Binding Path=InternalName, Mode=TwoWay}">
                                                            
                                                        
                                                    
<datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.Language}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <combobox
 itemssource="{Binding DataContext.ReceipientLanguages, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}" 
="" selectedindex="0" selectedvalue="{Binding LanguageKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" selectedvaluepath="Identifier">
                                                            
                                                        
                                                    
<datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.NameDisplay}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <checkbox horizontalalignment="Center" ischecked="{Binding IsNameDisplay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                                            
                                                        
                                                    

                                                    <datagridtemplatecolumn header="{x:Static infrastructureLayer:Translations.AddressDisplay}">
                                                        <datagridtemplatecolumn.celltemplate>
                                                            <datatemplate>
                                                                <checkbox horizontalalignment="Center" ischecked="{Binding IsAddressDisplay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">


What I have tried:

Please help me on this. I am not getting how to achieve this
Posted
Updated 16-Mar-20 6:55am
v2
Comments
[no name] 16-Mar-20 10:41am    
Load all columns, then "hide" the ones they didn't want.
Maciej Los 16-Mar-20 12:51pm    
Short And To The Point!

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900