/// <summary> /// フォームロードイベント /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormToDo_Load(object sender, EventArgs e) { // 期限に今日の日付をセットしておく dateTimePickerDeadline.Value = DateTime.Now; // XMLファイル読み込み XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(XmlFilePath); CsvFilePath = xmlDoc.SelectSingleNode("csvfile/path").InnerText; CsvFileEncode = xmlDoc.SelectSingleNode("csvfile/encode").InnerText; // ToDoリストの保存済みデータがあれば読み込んでグリッドに表示 if (File.Exists(CsvFilePath)) { // ファイル操作用クラスのインスタンス生成 FileControl file = new FileControl(CsvFilePath, CsvFileEncode); // ファイル読み込み実行 string error = file.Read(); // 戻り値(リスト)をグリッドに追加 foreach (string line in file.Lines) { // 行を配列化 string[] toDoArray = line.Split(','); // グリッドに値をセット dataGridViewToDo.Rows.Add(false, toDoArray[0], toDoArray[1].Replace("<br>", "\r\n")); } if (error.Length != 0) { MessageBox.Show("保存済みデータ読み込みエラー:\r\n" + error); } } }
/// <summary> /// 保存ボタン押下時 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonSave_Click(object sender, EventArgs e) { DialogResult dr = MessageBox.Show("現在のリスト内容で保存しますか?\r\n完了チェックを入れたToDoは保存対象外となり、次回以降は表示されません。", "保存", MessageBoxButtons.YesNo); if (dr != DialogResult.Yes) { return; } string lines = ""; // グリッドの読み込み for (int i = 0; i < dataGridViewToDo.Rows.Count; i++) { for (int j = 0; j < dataGridViewToDo.Columns.Count; j++) { bool nextflg = false; switch (j) { case 0: if (dataGridViewToDo.Rows[i].Cells[j].Value.ToString() == "1") { nextflg = true; } break; case 1: lines += dataGridViewToDo.Rows[i].Cells[j].Value.ToString(); break; case 2: lines += ","; lines += dataGridViewToDo.Rows[i].Cells[j].Value.ToString().Replace("\r\n", "<br>"); lines += "\r\n"; break; } if (nextflg) { break; } } } // 保存開始 FileControl file = new FileControl(CsvFilePath, CsvFileEncode); string error = file.Write(lines); if (error.Length == 0) { MessageBox.Show("保存完了"); } else { MessageBox.Show(error); } }