DataGridView Selected Row Move UP and DOWN

后端 未结 13 1506
盖世英雄少女心
盖世英雄少女心 2020-12-06 01:31

How can I allow selected rows in a DataGridView (DGV) to be moved up or down. I have done this before with a ListView. Unfortunetly, for me, replacing the DGV is not an opt

13条回答
  •  鱼传尺愫
    2020-12-06 02:08

    SchlaWiener's answer worked well, and I just wanna add something to it:

    private void button1_Click(object sender, EventArgs e) //The button to move up
    {
        int position = bs.Position;
    
        //.......neglected.......
    
        dataGridView1.ClearSelection();
        dataGridView1.Rows[position].Selected = true;
        bs.MovePrevious();
    
    }
    

    Adding those 3 lines at the bottom to also make the selection move (both bindingSource and dataGridView), so that we can continuously click the bottom to move a row up.

    For moving down just call bs.MoveNext()

    (I have not enough reputation to post as comment yet)

提交回复
热议问题