cscの作法 その423

# 概要
cscの作法、調べてみた。
練習問題、やってみた。

# 練習問題
todoアプリを作れ。

# 方針

- .NETFramework2.0 
- datagridview使う。
- データの永続化は、csv
- いつまで,何をどうする
- 日付入力でカレンダー表示


# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/76827152-3604-f7ca-1396-728254159d81.png)

# サンプルコード

```

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data.OleDb;
using System.Data;
using System.IO;
using System.Text;
using System.Collections.Generic;

class form1: Form {
	MonthCalendar cal1;
	DataGridView gd0;
	int row;
	int coloum;
	form1() {
		Text = "ToDo";
		ClientSize = new Size(580, 400);
		gd0 = new DataGridView();
		gd0.Location = new Point(50, 20);
		gd0.Width = 500;
    	gd0.Height = 300;
		Controls.AddRange(new Control[] {
			gd0
		});
		Button btn1 = new Button();
		btn1.Location = new Point(50, 330);
		btn1.Text = "load";
		btn1.Click += btn1_Click;
		Controls.AddRange(new Control[] {
			btn1
		});
		Button btn2 = new Button();
		btn2.Location = new Point(150, 330);
		btn2.Text = "save";
		btn2.Click += btn2_Click;
		Controls.AddRange(new Control[] {
			btn2
		});
		gd0.CellClick += new DataGridViewCellEventHandler(OnChanged);
	}
	private void OnChanged(object sender, DataGridViewCellEventArgs e) {
		int ri = e.RowIndex;
		int ci = e.ColumnIndex;
		if (ci == 0)
		{
			this.cal1 = new System.Windows.Forms.MonthCalendar();
			this.cal1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.cal1_DateSelected);
			gd0.Controls.Add(cal1);
			cal1.Show();
			coloum = e.ColumnIndex;
			row = e.RowIndex;
		}
	}
	private void cal1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e) {
		gd0[coloum, row].Value = e.Start.ToShortDateString();
		(sender as MonthCalendar).Dispose();
	}
	void btn1_Click(object sender, System.EventArgs e) {
		Encoding enc = Encoding.GetEncoding("UTF-8");
		string file = "test2.csv";
		StreamReader reader = new StreamReader(file, enc);
		string temp = reader.ReadToEnd();
		reader.Close();
		string[] lines = temp.Split(new string[] {"\r\n"}, StringSplitOptions.None);
		string[] spLine;
		spLine = lines[0].Split(new char[] {','}, StringSplitOptions.None);
		gd0.ColumnCount = spLine.Length;
		for (int i = 0; i < spLine.Length; i++)
		{
			gd0.Columns[i].HeaderText = spLine[i].Trim('"');
		}
		DataGridViewRow[] rows = new DataGridViewRow[lines.Length - 2];
		for (int i = 1; i < lines.Length - 1; i++)
		{
			spLine = lines[i].Split(new char[] {','}, StringSplitOptions.None);
			for (int j = 0; j < spLine.Length; j++)
			{
				spLine[j] = spLine[j].Trim('"');
			}
			DataGridViewRow row = new DataGridViewRow();
			row.CreateCells(gd0);
			row.SetValues(spLine);
			rows[i - 1] = row;
		}
		gd0.Rows.AddRange(rows);
	}
	void btn2_Click(object sender, System.EventArgs e) {
		StreamWriter sw = new StreamWriter("test2.csv", false);
		sw.WriteLine("いつまで,何をどうする");
		int rowCount = gd0.Rows.Count;
		for (int i = 0; i < rowCount - 1; i++)
		{
			List<String> strList = new List<String>();
			for (int j = 0; j < gd0.Columns.Count; j++)
			{
				String s1 = (String) gd0[j, i].Value;
				String s2 = Convert.ToString(gd0[j, i].Value);
				strList.Add(s2);
			}
			String[] strArray = strList.ToArray();
			String strCsvData = String.Join(",", strArray);
			sw.WriteLine(strCsvData);
		}
		sw.Close();
	}
	[STAThread]
	public static void Main() {
		Application.Run(new form1());
	}
}





```

以上。