C#、VisualStudio初心者です。
データベースから特定の列のみ抜き取ったものをリストに格納し、DateGridViewに表示したいのですが、反映されません。
---Listの型になる独自のクラス Model--- private string date; private string time; //以下、コンストラクタ生成、またアクセッサメソッド(割愛) ---DBからリストに格納 Control--- public List<Model> Show(string day) { List<Model> list = new List<Model>(); command = connection.CreateCommand(); SqlTransaction transaction = connection.BeginTransaction(); command.Connection = connection; command.Transaction = transaction; command.CommandText = @"SELECT date,time from testtable Where date Like '" + day + "%' order by date asc;"; var reader = command.ExecuteReader(); while (reader.Read()) { date = (string)reader.GetValue(1); time = (string)reader.GetValue(2); } reader.Close(); transaction.Commit(); return list; } ---Modelからリストを受け取りDateGridViewに表示をする View--- private void Search_Click(object sender, EventArgs e) { try { string year = comboBox1.SelectedItem.ToString(); string month = comboBox2.SelectedItem.ToString(); string day = year +"/"+ month; list =control.Show(day); for (int i = 0; i < list.Count(); i++) { list.ForEach(o=>dataGridView1.Rows.Add(o.Date,o.Starttime,o.Finishtime,o.Sumtime)); } } catch (NullReferenceException ex) { Console.WriteLine(ex.Message); MessageBox.Show("未入力項目があります。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //インスタンス作成、初期化等は省略しています。
SQL文、Listの格納の仕方、DateGridViewに表示するくだり、
等間違えていそうなところはところどころあるのですが全くの無知なのでわかりません。
ご教授お願いいたします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/18 01:22