DataGridView 現在セルの取得・変更(サンプルあり)

📋 目次(クリックで展開)

Windows Forms で提供されている DataGridView の、現在セルの取得と変更の方法を紹介します。

現在セルの変更

DataGridView の CurrentCell プロパティに、セルを指定します。

private void Form1_Load(object sender, EventArgs e)
{
...
// 現在セルの設定
this.dataGridView1.CurrentCell = this.dataGridView1[1, 3];
}

現在セルの取得

DataGridView の現在セルの情報は、CurrentCell プロパティ配下の列インデックス(ColumnIndex)、行インデックス(RowIndex)、値(Value)などを利用することができます。

private void Form1_Load(object sender, EventArgs e)
{
...
Debug.WriteLine("列インデックス:" + this.dataGridView1.CurrentCell.ColumnIndex);
Debug.WriteLine("行インデックス:" + this.dataGridView1.CurrentCell.RowIndex);
Debug.WriteLine("値:" + this.dataGridView1.CurrentCell.Value);
}

実行結果

現在セルの情報が出力されていることが分かります。

サンプルのダウンロード

DataGridView_CurrentCell_CS.zip
※ サンプルは、.NET 8 を利用しています。


💬 コメント