Visual Studio/WPF > DataGrid > dataGrid_AutoGeneratingColumn()에서 숨겨진 열 설정

운영 환경
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

@ WPF 4.5 입문 by 오타 카즈키
No.3000/9985

WPF에서 테이블 형식의 데이터를 표시하는 컨트롤입니다.
...
ItemsSource 속성에 설정된 컬렉션의 요소를 한 줄로 표시합니다.

DataGrid의 자동 열 생성의 예 등을 알기 쉽게 소개되고 있다.

No.3090/9985
근처에서 dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 를 사용한 열 숨기기 설정 등의 예가 소개되고 있다.

시도해 보았다.

XAML
<Window x:Class="_170425_t1030_dataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170425_t1030_dataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Height="181" Margin="21,21,0,0" VerticalAlignment="Top" Width="465" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn"/>

    </Grid>
</Window>

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _170425_t1030_dataGrid
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var data = new ObservableCollection<Member>();
            data.Add(new Member { Name = "7of9", Species = Species.Borg, Age = 20, comment = "1"});
            data.Add(new Member { Name = "Janeway", Species = Species.Human, Age = 20, comment = "2" });
            data.Add(new Member { Name = "Odo", Species = Species.Unknown, Age = 20, comment = "3" });
            dataGrid.ItemsSource = data;
        }

        public enum Species
        {
            Unknown = 0,
            Human,
            Android,
            Borg,
        }
        public class Member
        {
            public string Name { get; set; }
            public Species Species { get; set; }
            public int Age { get; set; }
            public string comment { get; set; }
        }

        private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Name":
                    e.Column.Header = "名前";
                    e.Column.DisplayIndex = 0;
                    break;
                case "Species":
                    e.Column.Header = "種族";
                    e.Column.DisplayIndex = 1;
                    break;
                case "Age":
                    e.Cancel = true;
                    break;
                case "comment":
                    e.Column.Header = "備考";
                    e.Column.DisplayIndex = 2;
                    break;
            }
        }
    }

}

좋은 웹페이지 즐겨찾기