たるきちのあれこれ









  Topプログラミングサンプル ▶ DataGridView ▶ 選択している行・列・セルを取得、変更

選択している行・列・セルを取得するサンプルです。

C#  選択している行・列・セルを取得

// 選択しているセルの行(int)
DataGridView1.CurrentCell.RowIndex

// 選択しているセルの列(int)
DataGridView1.CurrentCell.ColumnIndex

// 選択しているセルの値(string)
DataGridView1.CurrentCell.Value.ToString()



選択している行・列・セルを変更するサンプルです。

C#  選択している行・列・セルを変更

// 選択セルを(0,0)に変更
DataGridView1[0, 0].Selected = true;

// 選択行を0に変更
DataGridView1.Rows[0].Selected = true;

// 選択列を0に変更
DataGridView1.Columns[0].Selected = true;

// 選択セルの値を「あいう」に変更
DataGridView1[DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex].Value = "あいう";