C# - Move DataGridView Row Up And Down

How To Move Selected Datagridview Row Up N Down In C#

Move DataGridView Row Up & Down In C#


In This C# Tutorial  We Will See How To Move Selected Datagridview Row Up Or Down On Button Click In CSharp Programming Language.


Project Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class datagridview_row_up_down : Form
    {
        public datagridview_row_up_down()
        {
            InitializeComponent();
        }

        DataTable table = new DataTable();
        int rowIndex;
        private void datagridview_row_up_down_Load(object sender, EventArgs e)
        {
            // populate dgv from datatable

            // add columns
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("First Name", typeof(string));
            table.Columns.Add("Last Name", typeof(string));
            table.Columns.Add("Age", typeof(int));

            // add rows
            table.Rows.Add(1, "First A", "Last A", 10);
            table.Rows.Add(2, "First B", "Last B", 20);
            table.Rows.Add(3, "First C", "Last C", 30);
            table.Rows.Add(4, "First D", "Last D", 40);
            table.Rows.Add(5, "First E", "Last E", 50);
            table.Rows.Add(6, "First F", "Last F", 60);
            table.Rows.Add(7, "First G", "Last G", 70);
            table.Rows.Add(8, "First H", "Last H", 80);

            dataGridView1.DataSource = table;

        }

        private void buttonUP_Click(object sender, EventArgs e)
        {
            // get selected row index
            rowIndex = dataGridView1.SelectedCells[0].OwningRow.Index;

            // create a new row
            DataRow row = table.NewRow();

            // add values to the new row
            row[0] = int.Parse(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());
            row[1] = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
            row[2] = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
            row[3] = int.Parse(dataGridView1.Rows[rowIndex].Cells[3].Value.ToString());

            if (rowIndex > 0) { 
                // delete the selected row
                table.Rows.RemoveAt(rowIndex);
                // add the new row 
                table.Rows.InsertAt(row, rowIndex - 1);
                dataGridView1.ClearSelection();
                // select the new row
                dataGridView1.Rows[rowIndex - 1].Selected = true;
            }

        }

        private void buttonDOWN_Click(object sender, EventArgs e)
        {
            rowIndex = dataGridView1.SelectedCells[0].OwningRow.Index;
            DataRow row = table.NewRow();

            row[0] = int.Parse(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());
            row[1] = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
            row[2] = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
            row[3] = int.Parse(dataGridView1.Rows[rowIndex].Cells[3].Value.ToString());

            if (rowIndex < dataGridView1.Rows.Count-2)
            {
                table.Rows.RemoveAt(rowIndex);
                table.Rows.InsertAt(row, rowIndex + 1);
                dataGridView1.ClearSelection();
                dataGridView1.Rows[rowIndex + 1].Selected = true;
            }
        }
    }
}


// OUTPUT :

C# Move DataGridView Row Up N Down




Share this

Related Posts

Previous
Next Post »

2 comments

comments
30 juin 2018 à 20:23 delete

Nice work dear, But i want to only select row then...?

Reply
avatar
25 septembre 2021 à 05:48 delete


thanks how to do it from sql with tools?

Reply
avatar