LoginSignup
4
4

More than 5 years have passed since last update.

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

Posted at

元ネタ

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

だいたい予想通りで、
int型 : 0
string型 : 空白
bool型 : false
birthday : 空白
で表示される。

Nullable型

int型 : 0bool型 : falseは厳密にはnullではなくただの初期値なのでNullable型にした場合の動きはどうなるのだろう。

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

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();
}

結果

nullなら問答無用で空白になる模様
2016-02-12.png

4
4
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
4
4