Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Attributes cannot be changed at run time hence you cannot dynamically hide columns using data annotations. But it is possible to dynamically hide columns. Here is an example which demonstrates it.</p> <p><strong>XAML</strong></p> <pre><code>&lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;DataGrid ItemsSource="{Binding AutoGenerateColumnDetails}" CanUserAddRows="False" CanUserDeleteRows="False"&gt; &lt;/DataGrid&gt; &lt;Button Grid.Row="1" Content="Refresh" Click="Button_Click" HorizontalAlignment="Left" Margin="5"/&gt; &lt;DataGrid Name="dg" Grid.Row="2" ItemsSource="{Binding TemplateList}" AutoGeneratingColumn="dg_AutoGeneratingColumn"&gt; &lt;/DataGrid&gt; &lt;/Grid&gt; </code></pre> <p><strong>Code behind</strong></p> <pre><code>using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; namespace AutoHideDGColumn { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { private ViewModel _ViewModel = null; public MainWindow() { InitializeComponent(); _ViewModel = new ViewModel(); this.DataContext = _ViewModel; } private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (_ViewModel.AutoGenerateColumnDetails.Where(d =&gt; d.HideColumn == true).Select(d =&gt; d.PropertyName).ToList().Contains(e.PropertyName)) e.Cancel = true; } private void Button_Click(object sender, RoutedEventArgs e) { // Reset to trigger AutoGeneratingColumn event this.dg.AutoGenerateColumns = false; this.dg.AutoGenerateColumns = true; } } public class ViewModel { private List&lt;Template&gt; _TemplateList; public List&lt;Template&gt; TemplateList { get { return _TemplateList; } set { _TemplateList = value; } } private List&lt;AutoGenerateColumnDetail&gt; _AutoGenerateColumnDetails; public List&lt;AutoGenerateColumnDetail&gt; AutoGenerateColumnDetails { get { return _AutoGenerateColumnDetails; } set { _AutoGenerateColumnDetails = value; } } public ViewModel() { AutoGenerateColumnDetails = typeof(Template).GetProperties().Select(p =&gt; new AutoGenerateColumnDetail() { PropertyName = p.Name }).ToList(); TemplateList = new List&lt;Template&gt;() { new Template() { County = "Count1", Field = "Field1", Operator = "Operator1"}, new Template() { County = "Count2", Field = "Field2", Operator = "Operator2"}, new Template() { County = "Count3", Field = "Field2", Operator = "Operator3"}, }; } } public class Template { public string County { get; set; } public string Operator { get; set; } public string Field { get; set; } } public class AutoGenerateColumnDetail { public string PropertyName { get; set; } public bool HideColumn { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload