作成日: 2014/11/05 最終更新日: 2014/11/05
文書種別
使用方法
詳細
Enterキーを押した際に下のセルにカレントセルを移動するには、DataGridのKeyDownイベントでCurrentRowプロパティ等の設定をする必要があります。
◎サンプルコード(Visual Basic)
◎サンプルコード(C#)
◎サンプルコード(Visual Basic)
Public Sub New()
InitializeComponent()
Me.c1DataGrid1.ItemsSource = New StockCollection()
Me.c1DataGrid1.SelectionMode = C1.WPF.DataGrid.DataGridSelectionMode.MultiRange
AddHandler Me.c1DataGrid1.KeyDown, AddressOf c1DataGrid1_KeyDown
End Sub
Private Sub c1DataGrid1_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs)
If e.Key = System.Windows.Input.Key.Enter Then
e.Handled = True
If c1DataGrid1.SelectedIndex <> c1DataGrid1.Rows.Count - 1 Then
Dim r As Integer = c1DataGrid1.SelectedIndex + 1
' カレント行変更
c1DataGrid1.CurrentRow = c1DataGrid1.Rows(r)
' 選択範囲変更
c1DataGrid1.Selection.Clear()
c1DataGrid1.Selection.Add(c1DataGrid1(c1DataGrid1.CurrentRow.Index, c1DataGrid1.CurrentColumn.Index))
' スクロール設定
c1DataGrid1.ScrollIntoView(r, 0)
End If
End If
End Sub
InitializeComponent()
Me.c1DataGrid1.ItemsSource = New StockCollection()
Me.c1DataGrid1.SelectionMode = C1.WPF.DataGrid.DataGridSelectionMode.MultiRange
AddHandler Me.c1DataGrid1.KeyDown, AddressOf c1DataGrid1_KeyDown
End Sub
Private Sub c1DataGrid1_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs)
If e.Key = System.Windows.Input.Key.Enter Then
e.Handled = True
If c1DataGrid1.SelectedIndex <> c1DataGrid1.Rows.Count - 1 Then
Dim r As Integer = c1DataGrid1.SelectedIndex + 1
' カレント行変更
c1DataGrid1.CurrentRow = c1DataGrid1.Rows(r)
' 選択範囲変更
c1DataGrid1.Selection.Clear()
c1DataGrid1.Selection.Add(c1DataGrid1(c1DataGrid1.CurrentRow.Index, c1DataGrid1.CurrentColumn.Index))
' スクロール設定
c1DataGrid1.ScrollIntoView(r, 0)
End If
End If
End Sub
◎サンプルコード(C#)
public KB81042()
{
InitializeComponent();
// テストデータ設定
this.c1DataGrid1.ItemsSource = new StockCollection();
this.c1DataGrid1.SelectionMode = C1.WPF.DataGrid.DataGridSelectionMode.MultiRange;
this.c1DataGrid1.KeyDown += c1DataGrid1_KeyDown;
}
void c1DataGrid1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
e.Handled = true;
if (c1DataGrid1.SelectedIndex != c1DataGrid1.Rows.Count - 1)
{
var r = ++c1DataGrid1.SelectedIndex;
// カレント行変更
c1DataGrid1.CurrentRow = c1DataGrid1.Rows[r];
// 選択範囲変更
c1DataGrid1.Selection.Clear();
c1DataGrid1.Selection.Add(c1DataGrid1[c1DataGrid1.CurrentRow.Index, c1DataGrid1.CurrentColumn.Index]);
// スクロール設定
c1DataGrid1.ScrollIntoView(r, 0);
}
}
}
{
InitializeComponent();
// テストデータ設定
this.c1DataGrid1.ItemsSource = new StockCollection();
this.c1DataGrid1.SelectionMode = C1.WPF.DataGrid.DataGridSelectionMode.MultiRange;
this.c1DataGrid1.KeyDown += c1DataGrid1_KeyDown;
}
void c1DataGrid1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
e.Handled = true;
if (c1DataGrid1.SelectedIndex != c1DataGrid1.Rows.Count - 1)
{
var r = ++c1DataGrid1.SelectedIndex;
// カレント行変更
c1DataGrid1.CurrentRow = c1DataGrid1.Rows[r];
// 選択範囲変更
c1DataGrid1.Selection.Clear();
c1DataGrid1.Selection.Add(c1DataGrid1[c1DataGrid1.CurrentRow.Index, c1DataGrid1.CurrentColumn.Index]);
// スクロール設定
c1DataGrid1.ScrollIntoView(r, 0);
}
}
}
関連情報
旧文書番号
81042