【问题标题】:Datagridview - How to set the arrow of selected row?Datagridview - 如何设置选定行的箭头?
【发布时间】:2018-02-05 10:33:24
【问题描述】:

我有一个 DataGridView,我在其中为行创建了 MoveUp 和 MoveDown 函数。但是当我交换两行并更改所选行时,尽管我将所选行设置在交换行上,但所选行的箭头仍停留在前一个位置。我可以更改正确行上的箭头吗?这只是一个小细节,但我想知道是否可以做到。

我有这个代码:

    private void moveUp()
    {
        if (dataGridView1.RowCount > 0)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int rowCount = dataGridView1.Rows.Count;
                int index = dataGridView1.SelectedCells[0].OwningRow.Index;
                if (index == 0)
                {
                    return;
                }
                var temp = listApp[index];
                var temp2 = listApp[index - 1];
                listApp.Remove(temp);
                listApp.Remove(temp2);
                listApp.Insert(index-1, temp);
                listApp.Insert(index, temp2);
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = listApp;
                dataGridView1.SelectedCells[0].Selected = false;
                //dataGridView1.Rows[index].Selected = false;
                dataGridView1.Rows[index-1].Selected = true;
            }
        }
    }

这里是交换两行后的屏幕。

或者有更好的方法吗?

【问题讨论】:

    标签: c# datagridview datasource swap


    【解决方案1】:

    好的,我自己找到了解决方案。设置 dataGridView1.CurrentCell 就足够了。也许它对某人有用。 解决方法在这里:

        private void moveUp()
        {
            if (dataGridView1.RowCount > 0)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    int rowCount = dataGridView1.Rows.Count;
                    int index = dataGridView1.SelectedCells[0].OwningRow.Index;
                    if (index == 0)
                    {
                        return;
                    }
                    var temp = listApp[index];
                    var temp2 = listApp[index - 1];
                    listApp.Remove(temp);
                    listApp.Remove(temp2);
                    listApp.Insert(index-1, temp);
                    listApp.Insert(index, temp2);
                    dataGridView1.DataSource = null;
                    dataGridView1.DataSource = listApp;
                    dataGridView1.SelectedCells[0].Selected = false;                    
                    dataGridView1.Rows[index-1].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1[1, index - 1];
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 2015-12-29
      • 2020-11-05
      • 2013-04-04
      • 2010-11-11
      • 2011-05-29
      • 2020-05-21
      • 1970-01-01
      • 2013-10-21
      相关资源
      最近更新 更多