将焦点设置为DataGridView行

时间:2017-08-31 19:18:42

标签: c# datagridview

以下源代码旨在将焦点设置为已删除行的前一行。

enter image description here

假设我想从数据库中删除不需要的单词dddddddd。当我按下Delete按钮时,我希望将单词cynosure聚焦并放在DataGridView的顶部,而现在情况并非如此。

enter image description here

现在,它显示在底部。

enter image description here

源代码

    void SetFocusToWord(Word concernedWord)
    {
        if (concernedWord != null)
        {
            int index = 0;
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                Word item = r.Tag as Word;

                if (concernedWord.Name == item.Name)
                {
                    dataGridView1.Focus();
                    dataGridView1.CurrentCell = dataGridView1.Rows[index].Cells[0];

                    break;
                }

                index++;
            }
        }
    } 

private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    int selectionIndex = dataGridView1.SelectedRows[0].Index;

                    foreach (DataGridViewRow r in dataGridView1.SelectedRows)
                    {
                        Word c = r.Tag as Word;

                        if (c != null)
                        {
                            _wordDatabase.Delete(c);
                        }
                    }

                    LoadToDataGridView();

                    if(selectionIndex > 0)
                    {
                        selectionIndex = selectionIndex - 1;
                    }

                    Word item = dataGridView1.Rows[selectionIndex].Tag as Word;

                    SetFocusToWord(item);
                }
                else
                {
                    throw new Exception(SelectionErrorMessages.GetErrorMessageFor(typeof(Word)));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        } 

    void LoadToDataGridView()
    {
        dataGridView1.Rows.Clear();

        List<Word> items = (List<Word>)_wordDatabase.Get();

        if (items != null)
        {
            if (items.Count > 0)
            {
                int i = 0;
                foreach (Word c in items)
                {
                    dataGridView1.Rows.Add(c.Name, c.Hint);
                    dataGridView1.Rows[i].Tag = c;
                    ++i;
                }
            }
        }
    } 

2 个答案:

答案 0 :(得分:1)

重新加载数据库似乎是一个不必要的步骤。

根据您希望网格的行为方式,尝试使用以下代码:

if (dataGridView1.SelectedRows.Count > 0) {
  int selectIndex = dataGridView1.SelectedRows[0].Index;
  dataGridView1.Rows.RemoveAt(selectIndex);
  if (selectIndex > 0) {
    dataGridView1.ClearSelection();
    dataGridView1.Rows[selectIndex - 1].Selected = true;
    dataGridView1.FirstDisplayedScrollingRowIndex = selectIndex - 1;
  }
}

答案 1 :(得分:0)

您的项目中性能不佳和不必要的代码是您未正确使用winforms绑定的结果。我建议您相应地开始使用控件BindingContext和BindingSource。这可以解决你的大多数问题。

有关winforms绑定的更多详细信息,我建议使用此docs