Is there a way to hide a specific column in a DataGrid when AutoGenerateColumns=True?
I have a WPF 4.0 DataGrid that is bound to a DataTable using AutoGenerateColumns=True. The columns are dynamic, however I know there is always going to be a column named ID and I would like to hide 开发者_JAVA百科this column. Is there a way I can do this?
in your datagrid, subscribe for the AutoGeneratingColumn
event, the event args
(DataGridAutoGeneratingColumnEventArgs
) has the column name and a "Cancel
", if the column name is ID then set Cancel = true
. should do the trick.
You can use a behavior (reusable code) to do the job... This way you can use attribute which would centralize the column visibility in one place.
Usage:
<Window
...
xmlns:extension="clr-namespace:WpfControlLibrary.Extension;assembly=WpfControlLibrary">
<DataGrid ...
extension:DataGridBehavior.UseBrowsableAttributeOnColumn="True">
...
public class YourObjectItem
{
[Browsable(false)]
public Assembly Assembly { get; set; }
Code:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace HQ.Wpf.Util.Behaviors
{
/// <summary>
/// Using this behavior on a dataGRid will ensure to display only columns with "Browsable Attributes"
/// </summary>
public static class DataGridBehavior
{
public static readonly DependencyProperty UseBrowsableAttributeOnColumnProperty =
DependencyProperty.RegisterAttached("UseBrowsableAttributeOnColumn",
typeof(bool),
typeof(DataGridBehavior),
new UIPropertyMetadata(false, UseBrowsableAttributeOnColumnChanged));
public static bool GetUseBrowsableAttributeOnColumn(DependencyObject obj)
{
return (bool)obj.GetValue(UseBrowsableAttributeOnColumnProperty);
}
public static void SetUseBrowsableAttributeOnColumn(DependencyObject obj, bool val)
{
obj.SetValue(UseBrowsableAttributeOnColumnProperty, val);
}
private static void UseBrowsableAttributeOnColumnChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var dataGrid = obj as DataGrid;
if (dataGrid != null)
{
if ((bool)e.NewValue)
{
dataGrid.AutoGeneratingColumn += DataGridOnAutoGeneratingColumn;
}
else
{
dataGrid.AutoGeneratingColumn -= DataGridOnAutoGeneratingColumn;
}
}
}
private static void DataGridOnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
var propDesc = e.PropertyDescriptor as PropertyDescriptor;
if (propDesc != null)
{
foreach (Attribute att in propDesc.Attributes)
{
var browsableAttribute = att as BrowsableAttribute;
if (browsableAttribute != null)
{
if (!browsableAttribute.Browsable)
{
e.Cancel = true;
}
}
// As proposed by "dba" stackoverflow user on webpage:
// https://stackoverflow.com/questions/4000132/is-there-a-way-to-hide-a-specific-column-in-a-datagrid-when-autogeneratecolumns
// I added few next lines:
var displayName = att as DisplayNameAttribute;
if (displayName != null)
{
e.Column.Header = displayName.DisplayName;
}
}
}
}
}
}
Other possibility would be Visibility.Collapsed
:
private void dataGrid_AutoGeneratingColumn(object sender,
DataGridAutoGeneratingColumnEventArgs e)
{
//Set properties on the columns during auto-generation
switch (e.Column.Header.ToString())
{
case "rownameYouWantToHide":
e.Column.Visibility = Visibility.Collapsed;
break;
}
}
I wouldn't say it's great solution... but... you can have one more abstraction layer for example let's say you have an object like:
public class Foo
{
public string Id { get; set; }
public string Property2 { get; set; }
public string Property3 { set; get; }
}
You don't want column for Id, so you create new object
public class Foo2
{
public string Property2 { get; set; }
public string Property3 { set; get; }
}
then map/convert Foo to Foo2 and you are done.
Another possible way (not always possible) is to change access modifier to internal
public class Foo
{
internal string Id { get; set; }
public string Property2 { get; set; }
public string Property3 { set; get; }
}
this way you won't have Id column generated either.
I achieved this using Browsable
attribute and Visibility: Collapsed
Model
class CourseModel
{
[Description("")]
[ReadOnly(false)]
public bool Select { get; set; } = true; // Checkbox
[Description("Course ID")]
[ReadOnly(true)]
[Browsable(false)]
public string ID { get; set; } // Hidden column
[Description("Course Title")]
[ReadOnly(true)]
public string Title { get; set; }
}
Custom control extension:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace MyProject.FrontEnd.Controls
{
class CustomDataGrid : DataGrid
{
// Take attributes of POCO, if available (https://stackoverflow.com/a/17255496/979621)
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
try
{
base.OnAutoGeneratingColumn(e);
var propertyDescriptor = e.PropertyDescriptor as PropertyDescriptor;
e.Column.Header = propertyDescriptor.Description;
e.Column.IsReadOnly = propertyDescriptor.IsReadOnly;
e.Column.Visibility = propertyDescriptor.IsBrowsable
? Visibility.Visible
: Visibility.Collapsed;
}
catch
{
// ignore; retain field defaults
}
}
}
}
ViewModel
public ObservableCollection<CourseModel> Courses { get; set; }
XAML
<Window
...
xmlns:controls="clr-namespace:MyProject.FrontEnd.Controls"
...
>
...
<controls:CustomDataGrid x:Name="courses"
ItemsSource="{Binding Path=Courses, Mode=TwoWay,
NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" />
I can't speak for 4, however it was not possible in 3.5 SP1, at least without registering for an event which I wanted to avoid at all costs.
What you could do instead is change your generation code to AutoGenerateColumns=False
then just place the columns you care about within the XAML as the underlying data will all still be placed within the columns appropriately
<dg:DataGridTextColumn Header="Display" Binding="{Binding DisplayName}"/>
<dg:DataGridTextColumn Header="Host" Binding="{Binding HostName}"/>
<dg:DataGridTextColumn Header="Database" Binding="{Binding Database}"/>
<dg:DataGridTextColumn Header="Username" Binding="{Binding Username}"/>
<dg:DataGridTextColumn Header="Password" Binding="{Binding Password}"/>
This will allow you to display the only columns you care about in relation to the underlying model as well as change the Header
to display as you see fit, so you are not tied to the Property
name on the model.
I've recently done this and wanted to share my solution:
I just made a view model I wanted the datagrid to follow and for the fields I wanted it to ignore (that is, not auto generate columns for), simply set those fields to private. Worked like a charm and there's no need for unnecessary code.
For example, here's the view model I pass to the view that contains the datagrid. I get all I need by simply setting the fields I don't want as columns to private, shown on the "FullPath" field in my example. I understand this may not be possible in every scenario, but worked for me quite well.
namespace dev
{
/// <summary>
/// View model for tag list view in data grid
/// </summary>
public class TagDataViewModel : BaseViewModel
{
/// <summary>
/// Default constructor
/// </summary>
/// <param name="path">The JSONPath to this item</param>
public TagDataViewModel(string path)
{
FullPath = path;
}
/// <summary>
/// Gets and sets the JSONPath to this item
/// </summary>
private string FullPath { get; set; }
/// <summary>
/// Gets the name
/// </summary>
public string Name => ProjectHelpers.GetPropertyValue(FullPath, "Name");
/// <summary>
/// Gets the address
/// </summary>
public string Address => ProjectHelpers.GetPropertyValue(FullPath, "Address");
/// <summary>
/// Gets the data type
/// </summary>
public string DataType => ProjectHelpers.GetPropertyValue(FullPath, "DataType");
/// <summary>
/// Gets the scan rate
/// </summary>
public string ScanRate => ProjectHelpers.GetPropertyValue(FullPath, "ScanRate");
/// <summary>
/// Gets the scaling type
/// </summary>
public string ScalingType => ProjectHelpers.GetPropertyValue(FullPath, "ScalingType");
}
}
精彩评论