c.sharp:datagridview_クリップボード
**以前のリビジョンの文書です**
.NET dataGridViewの操作方法
コマンド
// 全て削除
dataGridView.Rows.Clear();
// 新規行
DataGridViewRow row = new DataGridViewRow();
row .CreateCells(this.dataGridView1); // (おまじない) ヘッダを作る
クリップボードから入力
- snippet.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BSCH3vPartisListMaker
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//ヘッダーとすべてのセルの内容に合わせて、列の幅を自動調整する
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
//ヘッダーとすべてのセルの内容に合わせて、列の幅を自動調整する
dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
}
private void DatagridView1_CleanAll()
{
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
}
private void button_Past_Click(object sender, EventArgs e)
{
//クリップボードの内容を取得して、行で分ける
string pasteText = Clipboard.GetText();
if (string.IsNullOrEmpty(pasteText)) return;
pasteText = pasteText.Replace("\r\n", "\n");
pasteText = pasteText.Replace('\r', '\n');
pasteText = pasteText.TrimEnd(new char[] { '\n' });
string[] lines = pasteText.Split('\n');
foreach (string line in lines)
{
//タブで分割
string[] vals = line.Split('\t');
//列数が合っているか調べる
if (vals.Length != dataGridView1.ColumnCount)
{
MessageBox.Show($"列数が違います。{vals.Length}!={dataGridView1.ColumnCount}");
return;
}
// 新規行
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(this.dataGridView1);
//各セルの値を設定
for (int i = 0; i < row.Cells.Count; i++)
{
row.Cells[i].Value = vals[i];
}
// 追加
dataGridView1.Rows.Add(row);
}
}
private void Transfer()
{
// datagrid1の内容をdatagrid2に変換
//全ての行を削除
dataGridView2.Rows.Clear();
// 集計
//
int N = dataGridView1.Rows.Count;
for (int i = 0; i < N; i++)
{
var row1 = dataGridView1.Rows[i];
string part_name = dataGridView1.Rows[i].Cells[6].Value.ToString();
Debug.WriteLine($"part_name: {part_name}");
// dataGridView2に含まれているかを調べる
bool isContain = false;
int N2 = dataGridView2.Rows.Count;
for (int j = 1; j < N2; j++)
{
var row2 = dataGridView2.Rows[j];
if (part_name == row2.Cells[6].Value.ToString())
{
// 含まれている
row2.Cells[2].Value = (int.Parse(row2.Cells[2].Value.ToString()) + 1); // 個数は1
// パーツ名を追加
string cell_name = row2.Cells[1].Value.ToString() + row2.Cells[2].Value.ToString();
row2.Cells[7].Value += $", {cell_name}";
isContain = true;
}
}
if (isContain == false)
{
DataGridViewRow row2 = new DataGridViewRow();
row2.CreateCells(this.dataGridView2);
//各セルの値を設定
for (int j = 0; j < row2.Cells.Count; j++)
{
row2.Cells[j].Value = row1.Cells[j].Value;
}
// 行数取得
int Nr = dataGridView2.Rows.Count+1;
row2.Cells[0].Value = Nr.ToString();
// 個数(初期値1)
row2.Cells[2].Value = (1).ToString(); // 個数は1
// 素子名
string cell_name = row1.Cells[1].Value.ToString() + row1.Cells[2].Value.ToString();
row2.Cells[7].Value = cell_name; // 個数は1
// 追加
dataGridView2.Rows.Add(row2);
}
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button_CleanAll_Click(object sender, EventArgs e)
{
DatagridView1_CleanAll();
}
private void button_Transfer_Click(object sender, EventArgs e)
{
Transfer();
}
}
}
c.sharp/datagridview_クリップボード.1519785866.txt.gz · 最終更新: 2018/02/28 02:44 by fifi