0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

cscの作法 その29 datagrid

0
Last updated at Posted at 2020-01-03

概要

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

練習問題

datagridをcsvに書き込め。

サンプルコード

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 {
	DataGridView gd0;
	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
		});
	}
	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());
	}
}






以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?