概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
datagridのセルクリックでカレンダーを表示せよ。
写真
サンプルコード
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 = "DataGrid";
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("Shift_JIS");
string file = "test1.csv";
StreamReader reader = new StreamReader(file, enc);
string temp = reader.ReadToEnd();
string[] lines = temp.Split(new string[] {
"\r\n"
}, StringSplitOptions.None);
string[] spLine;
spLine = lines[0].Split(new char[] {
',',
'\t'
}, 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 - 1];
for (int i = 1; i < lines.Length; i++)
{
spLine = lines[i].Split(new char[] {
',' ,
'\t'
}, 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);
MessageBox.Show("ok");
}
void btn2_Click(object sender, System.EventArgs e) {
StreamWriter sw = new StreamWriter("test2.csv", false);
int rowCount = gd0.Rows.Count;
if (gd0.AllowUserToAddRows == true)
{
rowCount = rowCount - 1;
}
for (int i = 0; i < rowCount; 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();
MessageBox.Show("ok");
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。