在 C# 中使用 xeed datagrid 将焦点更改到下一个单元格

发布于 2024-12-02 07:09:29 字数 567 浏览 4 评论 0原文

我是 C Sharp 编程新手。我需要对我们的项目进行更改。基本上我们使用的是 Xeed 数据网格,它有 4 列。数据与集合对象绑定,并通过数据库调用动态更新。我的问题共 4 列,其中 1 列是可编辑的。当用户在此列中进行更改并按 Enter 键时,焦点需要在编辑模式下更改到同一列中的下方单元格。以下是我正在编写的 KeyUp 事件。在我更改此列并按 Enter 键后,焦点将转到下一行,但编辑模式不会转到下一个单元格,而是保留在已编辑的同一单元格上。

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

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.EndEdit();
    int currentRow = _dataGrid.SelectedIndex;
    currentRow++;
    _dataGrid.SelectedIndex = currentRow;
    _dataGrid.Focus() ;
    _dataGrid.BeginEdit();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-12-09 07:09:29

我认为您需要更改 CurrentItem 属性。我使用不同的网格控制,所以我不保证它会起作用。但程序应该是这样的:

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();
    }
}

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();
    }
}
如梦初醒的夏天 2024-12-09 07:09:29

遵循解决方案

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]);

    }
}

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]);

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文