开发者

changing the focus to next cell using xeed datagrid in c#

开发者 https://www.devze.com 2023-04-01 17:55 出处:网络
I am new to C sharp programming. I need to make a change in our project. Basically we are using Xeed datagrid, which has 4 columns. Data is bound with the collection object and was updated dynamically

I am new to C sharp programming. I need to make a change in our project. Basically we are using Xeed datagrid, which has 4 columns. Data is bound with the collection object and was updated dynamically with DB call. My question is out of 4 columns, 1 column is editable. when user make a change in this column and hit enter, the focus needs to change to below cell in the same column in the edit mode. Following is the KeyUp event I am writting. After I make change this columna nd hit enter the focus is going to next row, but the edit mode is not going to next cell, but instead stays on the same cell which was eddited.

private void _dataGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
    _dataGrid开发者_StackOverflow中文版.EndEdit();
    int currentRow = _dataGrid.SelectedIndex;
    currentRow++;
    _dataGrid.SelectedIndex = currentRow;
    _dataGrid.Focus() ;
    _dataGrid.BeginEdit();
    }
}


I think you need to change CurrentItem property. Iam using different grid control so I not guaranty that it will work. But procedure should be something like this:

private void _dataGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
       _dataGrid.EndEdit();
       int nextIndex = _dataGrid.SelectedIndex + 1;
       //should crash when enter hit after editing last row, so need to check it
       if(nextIndex < _dataGrid.items.Count)
       {
          _dataGrid.SelectedIndex = nextIndex;
          _dataGrid.CurrentItem = _dataGrid.Items[nextIndex];
        }
       _dataGrid.BeginEdit();
    }
}


Following the solution

private void _dataGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        int rowCount = _dataGrid.Items.Count;
        int currentRow = _dataGrid.SelectedIndex;

        if (rowCount - 1 > currentRow)
            currentRow++;
        else
            currentRow = 0;

        _dataGrid.CurrentItem = _dataGrid.Items[currentRow];
        _dataGrid.BringItemIntoView(_dataGrid.Items[currentRow]);

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消