データを表示するために DataGridView コントロールを使用する(メモリ内)、最小限の CRUD デモを完成させる:
DataGridViewExtentions.cs#
using System.Reflection;
namespace ConsoleApp.WindowsFormTests
{
public static class ExtensionMethods
{
// ダブルバッファリング機能を有効にし、UI コントロールの再描画時のちらつきを減らす
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo? pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi!.SetValue(dgv, setting, null);
}
}
}
FrmMain.cs#
using System.Data;
namespace ConsoleApp.WindowsFormTests
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private readonly DataTable _dataTable = new();
private void FrmMain_Load(object sender, EventArgs e)
{
InitialDataTable();
InitalDataGridView();
btnAdd.Click += BtnAdd_Click;
btnUpdate.Click += BtnUpdate_Click;
btnDelete.Click += BtnDelete_Click;
}
private void BtnDelete_Click(object? sender, EventArgs e)
{
if (!AnySelectedRow())
{
MessageBox.Show("削除する行を選択してください");
return;
}
var row = GetSelectedRow();
var id = row.Cells["Id"].Value;
var nickName = row.Cells["NickName"].Value;
var dialogResult = MessageBox.Show($"{nickName} を削除してもよろしいですか?", "削除確認", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No) return;
DeleteDataRow(id);
ClearDataGridViewSelection();
}
private void BtnUpdate_Click(object? sender, EventArgs e)
{
if (!AnySelectedRow())
{
MessageBox.Show("変更する行を選択してください");
return;
}
var nickName = txtNickName.Text.Trim();
if (string.IsNullOrWhiteSpace(nickName))
{
txtNickName.Focus();
return;
}
var id = GetSelectedRow().Cells["Id"].Value;
UpdateDataRow(id!, nickName);
}
private void BtnAdd_Click(object? sender, EventArgs e)
{
var nickName = txtNickName.Text.Trim();
if (string.IsNullOrWhiteSpace(nickName))
{
txtNickName.Focus();
return;
}
dataGridView1.ClearSelection();
AddDataRow(nickName);
// 初めてデータを追加すると、dataGridView は最初のデータを選択します。ここでデフォルトの選択をクリアします
if (_dataTable.Rows.Count == 1)
{
ClearDataGridViewSelection();
}
txtNickName.Clear();
txtNickName.Focus();
}
private void DataGridView1_SelectionChanged(object? sender, EventArgs e)
{
if (!AnySelectedRow()) return;
var selectedRow = GetSelectedRow();
txtNickName.Text = selectedRow.Cells["NickName"].Value.ToString();
}
private void AddDataRow(string nickName)
{
_dataTable.Rows.Add(Guid.NewGuid(), nickName);
}
private void UpdateDataRow(object? key, string nickName)
{
var row = _dataTable.Rows.Find(key);
row!["NickName"] = nickName;
}
private void DeleteDataRow(object? key)
{
var row = _dataTable.Rows.Find(key);
row!.Delete();
}
private void InitialDataTable()
{
var idColumn = _dataTable.Columns.Add("Id");
_dataTable.Columns.Add("NickName");
_dataTable.PrimaryKey = new DataColumn[] { idColumn };
}
private void InitalDataGridView()
{
dataGridView1.DataSource = _dataTable;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.DoubleBuffered(true);
dataGridView1.RowPrePaint += DataGridView1_RowPrePaint;
dataGridView1.SelectionChanged += DataGridView1_SelectionChanged;
}
private void ClearDataGridViewSelection()
{
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
}
private bool AnySelectedRow() => dataGridView1.CurrentRow != null;
private DataGridViewRow GetSelectedRow() => dataGridView1.CurrentRow;
// フォーカススタイルの描画を防ぎ、視覚効果を改善し、行がフォーカスなしでより一貫して見えるようにする
private void DataGridView1_RowPrePaint(object? sender, DataGridViewRowPrePaintEventArgs e) => e.PaintParts &= ~DataGridViewPaintParts.Focus;
}
}
FrmMain.Designer.cs#
namespace ConsoleApp.WindowsFormTests
{
partial class FrmMain
{
/// <summary>
/// 必要なデザイナ変数。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 使用中のリソースをクリーンアップします。
/// </summary>
/// <param name="disposing">管理リソースを解放する場合は true; それ以外は false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナサポートのために必要なメソッド - このメソッドの内容をコードエディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
txtNickName = new TextBox();
btnAdd = new Button();
dataGridView1 = new DataGridView();
btnUpdate = new Button();
btnDelete = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
SuspendLayout();
//
// txtNickName
//
txtNickName.Location = new Point(71, 47);
txtNickName.Name = "txtNickName";
txtNickName.Size = new Size(125, 27);
txtNickName.TabIndex = 0;
//
// btnAdd
//
btnAdd.Location = new Point(235, 44);
btnAdd.Name = "btnAdd";
btnAdd.Size = new Size(114, 33);
btnAdd.TabIndex = 1;
btnAdd.Text = "追加";
btnAdd.UseVisualStyleBackColor = true;
//
// dataGridView1
//
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView1.Dock = DockStyle.Bottom;
dataGridView1.Location = new Point(0, 116);
dataGridView1.Name = "dataGridView1";
dataGridView1.ReadOnly = true;
dataGridView1.RowHeadersWidth = 51;
dataGridView1.Size = new Size(710, 392);
dataGridView1.TabIndex = 2;
//
// btnUpdate
//
btnUpdate.Location = new Point(374, 44);
btnUpdate.Name = "btnUpdate";
btnUpdate.Size = new Size(114, 33);
btnUpdate.TabIndex = 3;
btnUpdate.Text = "更新";
btnUpdate.UseVisualStyleBackColor = true;
//
// btnDelete
//
btnDelete.Location = new Point(513, 44);
btnDelete.Name = "btnDelete";
btnDelete.Size = new Size(114, 33);
btnDelete.TabIndex = 4;
btnDelete.Text = "削除";
btnDelete.UseVisualStyleBackColor = true;
//
// FrmMain
//
AcceptButton = btnAdd;
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(710, 508);
Controls.Add(btnDelete);
Controls.Add(btnUpdate);
Controls.Add(dataGridView1);
Controls.Add(btnAdd);
Controls.Add(txtNickName);
FormBorderStyle = FormBorderStyle.FixedSingle;
Name = "FrmMain";
StartPosition = FormStartPosition.CenterScreen;
Text = "メイン";
Load += FrmMain_Load;
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private TextBox txtNickName;
private Button btnAdd;
private DataGridView dataGridView1;
private Button btnUpdate;
private Button btnDelete;
}
}