LoginSignup
1
2

More than 5 years have passed since last update.

[.NET] DataGridViewのDataSourceにnullを持つObjectArrayを入れたらどう表示されるか

Last updated at Posted at 2016-02-11

DataGridViewのDataSourceにnull値を持つObjectArrayを入れたらどのように表示されるのか試してみた。

コード

まず、Personクラスを用意。
今回試すのはint型、string型、bool型、DateTime型にした。

    class Person
    {
        public int id { get; set; }
        public string name { get; set; }
        public bool sex { get; set; }
        public DateTime birthday { get; set; }
    }

次に、値を設定し、DataGridViewを表示。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var persons = new List<Person>();
            persons.Add(new Person() { id = 1, name = "鹿目まどか", birthday = new DateTime(2010, 10, 3), sex = false });
            persons.Add(new Person() { id = 2, name = "暁美ほむら", sex = false });
            persons.Add(new Person() { id = 3, name = null, sex = false });
            persons.Add(new Person() { id = 4, name = "巴マミ" });
            persons.Add(new Person() { id = 5, name = "佐倉杏子", sex = false });
            persons.Add(new Person() { name = "キュゥべえ", sex = true });

            dataGridView1.DataSource = persons.ToArray();
        }
    }

結果

キャプチャ.PNG

まとめ

だいたい予想通りで、

  • int型 : 0
  • string型 : 空白
  • bool型 : false
  • birthday : 空白

で表示される。

1
2
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
1
2