ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# DataGridView 表格行(Row) 上下移动

2022-03-01 15:03:18  阅读:421  来源: 互联网

标签:relevant C# private DataGridView blog int dataGridView1 selectionIdx Row


https://blog.csdn.net/plato_2/article/details/17434715?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2.pc_relevant_default&utm_relevant_index=4

 

好像有用吧,但是太麻烦了

 

https://blog.csdn.net/xiaotaibai2008/article/details/8099348

 

(1)首先在form中添加一个DataGridView控件,将默认AllowDrop=false 的属性设置为True,否侧不能拖动!
(2)对DataGridView的对象实现非数据源的绑定,因为设置DataSource属性即当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行。

2.代码准备
(1)控制移动时鼠标的图形,否则是一个禁止移动的标识
        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

(2)控制拖动的条件,也可以自行放宽条件
        private void  dataGridView1 _CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.Clicks < 2) && (e.Button == MouseButtons.Left))
            {
                if ((e.ColumnIndex == -1) && (e.RowIndex > -1))
                    dataGridView1.DoDragDrop(dataGridView1.Rows[e.RowIndex], DragDropEffects.Move);
            }
        }

(3)拖动后实现行的删除和添加,实现行交换位置的错觉
        int selectionIdx = 0;
        private void  dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            int idx = GetRowFromPoint(e.X, e.Y);

            if (idx < 0) return;

            if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                DataGridViewRow row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                dataGridView1.Rows.Remove(row);
                selectionIdx = idx;
                dataGridView1.Rows.Insert(idx, row);
            }
        }

        private int GetRowFromPoint(int x, int y)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                Rectangle rec = dataGridView1.GetRowDisplayRectangle(i, false);

                if (dataGridView1.RectangleToScreen(rec).Contains(x, y))
                    return i;
            }

            return -1;
        }

(4)控制被移动的行始终是选中行
        private void kryptonDataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            if (selectionIdx > -1)
            {
                dataGridView1.Rows[selectionIdx].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[selectionIdx].Cells[0];
            }
        }
————————————————
版权声明:本文为CSDN博主「xiaotaibai2008」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiaotaibai2008/article/details/8099348

 

标签:relevant,C#,private,DataGridView,blog,int,dataGridView1,selectionIdx,Row
来源: https://www.cnblogs.com/LuoCore/p/15950150.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有