0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio / WPF > DataGrid > エラー:列数が多い > DataGridView1.AutoGenerateColumns = Falseをする

Last updated at Posted at 2017-04-21
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

ComboBoxItemを設定しようとしていて、エラーが出ていた。

参考: http://blog.okazuki.jp/entry/20130224/1361693816

作ったコード

XAML
<Window x:Class="_170421_t1510_WpfDataGridComboBox.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:_170421_t1510_WpfDataGridComboBox"
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    
    <Grid>
        <DataGrid x:Name="dataGridView1" HorizontalAlignment="Left" Height="211" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridComboBoxColumn Header="Gender" 
                        SelectedValueBinding="{Binding Gender}"
                        DisplayMemberPath="Label" 
                        SelectedValuePath="Value">
                    <DataGridComboBoxColumn.ItemsSource>
                        <x:Array Type="{x:Type local:GenderComboBoxItem}">
                            <local:GenderComboBoxItem Label="未選択" Value="None" />
                            <local:GenderComboBoxItem Label="男" Value="Men" />
                            <local:GenderComboBoxItem Label="女" Value="Women" />
                        </x:Array>
                    </DataGridComboBoxColumn.ItemsSource>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>

    </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 _170421_t1510_WpfDataGridComboBox
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<User> users = new List<User>();
            users.Add(new User() { Name = "7of9", Gender = "Women" });
            users.Add(new User() { Name = "Chakotay", Gender = "Men" });
            users.Add(new User() { Name = "Odo", Gender = "None" });
            dataGridView1.ItemsSource = users;
        }
    }
    public class User
    {
        public string Name { get; set; }
        public string Gender { get; set; }
    }
    public enum Gender
    {
        None,
        Men,
        Women,
    }
    public class GenderComboBoxItem
    {
        public string Label { get; set; }
        public Gender Value { get; set; }
    }
}

work.png

  • NameとGenderの列が複数になってしまう。

エラー修正

参考:https://social.msdn.microsoft.com/Forums/vstudio/en-US/caa1c3b1-d0f0-4973-a68c-422da9c2966a/datagridview-shows-too-many-columns?forum=vbgeneral

DataGridView1.AutoGenerateColumns = False

上記を入れると列が多い問題はなくなった。

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 _170421_t1510_WpfDataGridComboBox
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<User> users = new List<User>();
            users.Add(new User() { Name = "7of9", Gender = "Women" });
            users.Add(new User() { Name = "Chakotay", Gender = "Men" });
            users.Add(new User() { Name = "Odo", Gender = "None" });
            dataGridView1.AutoGenerateColumns = false; /* 1 */
            dataGridView1.ItemsSource = users;
        }
    }
    public class User
    {
        public string Name { get; set; }
        public string Gender { get; set; }
    }
    public enum Gender
    {
        None,
        Men,
        Women,
    }
    public class GenderComboBoxItem
    {
        public string Label { get; set; }
        public Gender Value { get; set; }
    }
}

work.png

work.png

http://qiita.com/7of9/items/523cdcb42d7ea689d392
のv0.1で出ていたエラーは上記で出なくなっている。
レコードの新規追加もできる。

参考にしていた
http://blog.okazuki.jp/entry/20130224/1361693816
においてはXAMLファイルにて以下のようにfalse設定していた。

<DataGrid Name="dataGrid" AutoGenerateColumns="False">
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?