Creating Draggable Datagridview Table Item on Visual C#

Admin
0
In this tutorial, we will show you the code how to create draggable table content on datagridview so that they can be re-arranged as purpose. Here is the screenshot of the results. 
Let's jump to the code. 
Please note, before implementing the code you must :

1. Create new Windows form application project with the name : "WindowsFormsApplication1"
2. Add datagridview control and rename it as "dataGridView1".

Here is the full 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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 5;
            for (int i=0; i <5; i++) {
                string[] row = new string[] { "Column 0/" + "Row " + i.ToString(), "Column 1/" + "Row " + i.ToString(), "Column 2/" + "Row " + i.ToString() };
                dataGridView1.Rows.Add(row);
            }

            this.dataGridView1.AllowDrop = true;
           
            dataGridView1.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
            dataGridView1.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);
            dataGridView1.MouseUp += new MouseEventHandler(dataGridView1_MouseUp);

        }


        int dragRow = -1;
        Label dragLabel = new Label();

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                return;
            }
            if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
            dragRow = e.RowIndex;
            if (dragLabel == null) dragLabel = new Label();
            dragLabel.AutoSize = true;
            dragLabel.Text = "Dragging : " + dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
            dragLabel.Parent = dataGridView1;
            dragLabel.Location = e.Location;
        }

        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                return;
            }
            if (e.Button == MouseButtons.Left && dragLabel != null)
            {
                dragLabel.Location = e.Location;
                dataGridView1.ClearSelection();
            }
        }

        private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                return;
            }
            var hit = dataGridView1.HitTest(e.X, e.Y);
            int dropRow = -1;
            if (hit.Type != DataGridViewHitTestType.None)
            {
                dropRow = hit.RowIndex;
                if (dragRow >= 0)
                {
                    int tgtRow = dropRow + (dragRow > dropRow ? 1 : 0);
                    if (tgtRow != dragRow)
                    {
                        DataGridViewRow row = dataGridView1.Rows[dragRow];
                        dataGridView1.Rows.Remove(row);
                        dataGridView1.Rows.Insert(tgtRow, row);

                        dataGridView1.ClearSelection();
                        row.Selected = true;
                    }
                }
            }
            else { dataGridView1.Rows[dragRow].Selected = true; }

            if (dragLabel != null)
            {
                dragLabel.Dispose();
                dragLabel = null;
            }

        }

    }
}
    
    
Okay its finished. Feel free to share to support this page. Thank you.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Disclaimer : Content provided on this page is for general informational purposes only. We make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top