DOBON.NET プログラミング道: .NET Framework, VB.NET, C#, Visual Basic, Visual Studio, インストーラ, ...

DOBON.NET

DataGridの現在のセルを取得、設定する

DataGridの現在のセル(フォーカスのあるセル)はDataGridクラスのCurrentCellプロパティで取得、及び設定できます。

次の例ではDataGrid1(DataGridオブジェクト)の現在のセルを取得し、その行、列数、値を表示しています。

[VB.NET]
'現在のセルを取得
Dim c As DataGridCell = DataGrid1.CurrentCell
'セルの情報を表示
Dim rn As Integer = c.RowNumber
Console.WriteLine("選択されているセルの行:{0}", rn)
Dim cn As Integer = c.ColumnNumber
Console.WriteLine("選択されているセルの列:{0}", cn)
Console.WriteLine("選択されているセルの値:{0}", CStr(DataGrid1(rn, cn)))
[C#]
//現在のセルを取得
DataGridCell c = dataGrid1.CurrentCell;
//セルの情報を表示
int rn = c.RowNumber;
Console.WriteLine("選択されているセルの行:{0}", rn);
int cn = c.ColumnNumber;
Console.WriteLine("選択されているセルの列:{0}", cn);
Console.WriteLine("選択されているセルの値:{0}", dataGrid1[rn, cn].ToString());

次に現在のセルを(0,0)にする例を示します。

[VB.NET]
'現在のセルを(0,0)に設定
DataGrid1.CurrentCell = New DataGridCell(0, 0)
[C#]
//現在のセルを(0,0)に設定
DataGrid1.CurrentCell = new DataGridCell(0, 0);