C# windows datagridview表格行上下拖动示例
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
在C# Windows应用程序开发中,`DataGridView`是一个常用的控件,用于显示数据表格。这个控件提供了丰富的功能,包括编辑、排序、分页等。在某些场景下,用户可能需要手动调整行的顺序,比如在任务管理器或日程表应用中。本示例将介绍如何实现`DataGridView`的行上下拖动功能。 我们要创建一个新的Windows Forms项目,并添加一个`DataGridView`控件到主窗体(MainForm)。在`MainForm.Designer.cs`文件中,会自动生成`dataGridView1`的声明和初始化代码。 接着,我们需要监听`MouseDown`和`MouseMove`事件,以便在用户点击并拖动行时获取正确的行为。在`MainForm.cs`文件中,添加以下代码: ```csharp private void dataGridView1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y); if (hit.Type == DataGridViewHitTestType.Cell && hit.ColumnIndex >= 0 && hit.RowIndex >= 0) { selectedRowIndex = hit.RowIndex; dataGridView1.DoDragDrop(dataGridView1.Rows[selectedRowIndex], DragDropEffects.Move); } } } private void dataGridView1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(DataGridViewRow))) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; } private void dataGridView1_DragOver(object sender, DragEventArgs e) { Point pt = dataGridView1.PointToClient(new Point(e.X, e.Y)); DataGridView.HitTestInfo hit = dataGridView1.HitTest(pt.X, pt.Y); if (hit.Type == DataGridViewHitTestType.RowHeader && hit.RowIndex >= 0 && hit.RowIndex != selectedRowIndex) { e.Effect = DragDropEffects.Move; int oldIndex = selectedRowIndex; selectedRowIndex = hit.RowIndex; if (oldIndex < selectedRowIndex) { dataGridView1.Rows.Insert(oldIndex, dataGridView1.Rows[oldIndex + 1]); dataGridView1.Rows.RemoveAt(oldIndex + 1); } else { dataGridView1.Rows.Insert(oldIndex + 1, dataGridView1.Rows[oldIndex]); dataGridView1.Rows.RemoveAt(oldIndex); } dataGridView1.CurrentCell = dataGridView1.Rows[selectedRowIndex].Cells[0]; } else { e.Effect = DragDropEffects.None; } } ``` 这里的`selectedRowIndex`变量用于存储用户当前拖动的行索引。`MouseDown`事件检测到左键点击时,如果用户点击的是单元格,就启动拖放操作。`DragEnter`和`DragOver`事件处理拖放过程中行的插入和删除,确保行在正确的位置上。 此外,我们还需要在`MainForm_Load`事件中禁用自动调整行高度的功能,因为这可能会影响行拖动的视觉效果: ```csharp private void MainForm_Load(object sender, EventArgs e) { dataGridView1.AllowUserToAddRows = false; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; dataGridView1.RowTemplate.Height = 25; // 可以根据需要设置行高 } ``` 为了使项目能够编译和运行,还需要在`Program.cs`中设置程序入口点,并确保解决方案文件`DragRow.sln`中包含所有必要的引用和配置。 在项目中,`ReadMe.txt`可能包含了关于示例的简要说明,例如如何运行程序、注意事项以及可能的修改建议。`bin`和`Properties`目录则包含了编译后的可执行文件和项目设置。 实现`DataGridView`行上下拖动功能的关键在于监听鼠标事件,并在事件处理器中调整行的顺序。通过这样的方式,我们可以为用户提供更直观和友好的交互体验。在实际开发中,可以进一步完善这个功能,例如添加动画效果、错误处理和状态提示。
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
- 1
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![kz](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![iso](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 0
- 资源: 1
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0