前提・実現したいこと
研修の一環として、C# .NETを用い、データベースからデータを取得してWindows Formに表示させるアプリケーションを作成しています。
そこで、データベースから取得したデータを独自クラスを実装したListに格納し、それをDataGridViewへバインドさせようと考え、実装を行っています。
発生している問題・エラーメッセージ
データベースからデータの取得を行うこと自体はできているのですが、そのデータをDataGridViewに表示させることができていません。
(エラーメッセージはありません。)
該当のソースコード
** SearchForm.CS **
C#
1 //これより上の内容は省略しています。 2 3 connection.Open(); 4 SqlDataReader reader = command.ExecuteReader(); 5 6 //データの取得とリストへの反映 7 while (reader.Read()) 8 { 9 int id = (int)reader.GetValue(0); 10 string name = (string)reader.GetValue(1); 11 string zipcode = (string)reader.GetValue(2); 12 string address = (string)reader.GetValue(3); 13 string tel = (string)reader.GetValue(4); 14 short temp = (short)reader.GetValue(5); 15 string sex = ""; 16 17 switch (temp) 18 { 19 case 1: 20 sex = "男性"; 21 break; 22 case 2: 23 sex = "女性"; 24 break; 25 case 3: 26 sex = "その他"; 27 break; 28 default: 29 sex = "未登録"; 30 break; 31 } 32 33 customers.Add(new Customers(id, name, zipcode, address, tel, sex)); 34 } 35 36 connection.Close(); 37 customersBindingSource = new BindingSource 38 { 39 DataSource = customers 40 }; 41 dataGridView1.DataSource = customersBindingSource;
Customer.cs
C#
1 class Customers 2 { 3 private int Id { get; set; } 4 private string Name { get; set; } 5 private string Zipcode { get; set; } 6 private string Address { get; set; } 7 private string Tel { get; set; } 8 private string Sex { get; set; } 9 10 public Customers(int Id, string Name, string Zipcode, string Address, string Tel, string Sex) 11 { 12 this.Id = Id; 13 this.Name = Name; 14 this.Zipcode = Zipcode; 15 this.Address = Address; 16 this.Tel = Tel; 17 this.Sex = Sex; 18 } 19 }
試したこと
上記の実装を行う前に、一度「Fill」を使った方法を試しています。しかしDataGridViewに表示できない現象は同様に発生しており、解決に至っていません。
また、DataGridViewのDataSourceをGUI上から「Customer」に設定して試しましたが、同様の結果でした。
そのた
OS:Windows 10
DB:SQL Server 2017(LocalDB)
言語:C#
フレームワーク: .NET Framework 4.5.2
IDE:Visual Studio Community 2017
ご回答いただけたら幸いです。よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/06/22 02:30