DataGridView Selected Row Move UP and DOWN

后端 未结 13 1460
盖世英雄少女心
盖世英雄少女心 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:19

    Try this:

        private void buttonX9_Click(object sender, EventArgs e)//up
        {
    
            DataGridViewX grid = dataGridViewX1;
            try
            {
                int totalRows = grid.Rows.Count;
                int idx = grid.SelectedCells[0].OwningRow.Index;
                if (idx == 0)
                    return;
                int col = grid.SelectedCells[0].OwningColumn.Index;
                DataGridViewRowCollection rows = grid.Rows;
                DataGridViewRow row = rows[idx];
                rows.Remove(row);
                rows.Insert(idx - 1, row);
                grid.ClearSelection();
                grid.Rows[idx - 1].Cells[col].Selected = true;
    
            }
            catch { }
    
        }
    
    0 讨论(0)
提交回复
热议问题