惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
量子位
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Google DeepMind News
Google DeepMind News
小众软件
小众软件
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - CsharpFish

Sql2005 PIVOT运算符的操作 Lodop使用之WEB套打程序开发 jquery插件treeTable Asp.net 后台添加CSS、JS、Meta标签的写法 CodeSmith----SchemaExplorer类结构详细介绍 - CsharpFish - 博客园 js request 应用举例 背景图片随窗口拉伸 WebBrowser网页局部打印 - CsharpFish - 博客园 js日期控件-梅花雨 利用VML生成柱状图和饼图 js操作Table绑定DataSet 小小个人消费管理系统(已完善) GridView分页用户自定义控件 DbType与OleDbType 添加修改后返回刷新查询页面 asp.net页面传值(transfer Context.Handler) GridViev皮肤(Skin&CSS) JavaScript验证控件并阻止表单提交 GridView自定义分页存储过程
DataGridView拖动换行
CsharpFish · 2011-06-16 · via 博客园 - CsharpFish

1. 功能

  鼠标点击行首拖动换行

  多行选择删除

2. 主要代码

View Code

        private void dgv_SelectionChanged(object sender, EventArgs e)
        {
            
if (dgv.Rows.Count > 0 && selectionIdx > -1 && selectionIdx < dgv.Rows.Count - 1)// (dgv.SelectedRows.Count > 0))
            {if (dgv.Rows.Count <= selectionIdx)
                    selectionIdx 
= dgv.Rows.Count - 1;
                dgv.Rows[selectionIdx].Selected 
= true;
                
//dgv.CurrentCell = dgv.Rows[selectionIdx].Cells[0];
            }
        }
private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            
if (e.RowIndex >= 0)
                selectionIdx 
= e.RowIndex;
        }
private void dgv_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            
if ((e.Clicks < 2&& (e.Button == MouseButtons.Left))
            {
                
if ((e.ColumnIndex == -1&& (e.RowIndex > -1))
                    dgv.DoDragDrop(dgv.Rows[e.RowIndex], DragDropEffects.Move);
            }
        }
private void dgv_DragDrop(object sender, DragEventArgs e)
        {
            
int idx = GetRowFromPoint(e.X, e.Y);
            
if (idx < 0return;if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
            {
                DataGridViewRow row 
= (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                DataRow ddr 
= ((DataRowView)row.DataBoundItem).Row;
                DataRow nr 
= ((DataTable)dgv.DataSource).NewRow();for (int i = 0; i < row.Cells.Count; i++)
                {
                    nr[i] 
= row.Cells[i].Value;
                }
                selectionIdx 
= idx;
                ((DataTable)dgv.DataSource).Rows.InsertAt(nr, idx);
                ((DataTable)dgv.DataSource).Rows.Remove(ddr);
                dgv.ClearSelection();
            }
        }
private void dgv_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect 
= DragDropEffects.Move;
        }
private int GetRowFromPoint(int x, int y)
        {
            
for (int i = 0; i < dgv.RowCount; i++)
            {
                Rectangle rec 
= dgv.GetRowDisplayRectangle(i, false);if (dgv.RectangleToScreen(rec).Contains(x, y))
                    
return i;
            }
return -1;
        }

  源码下载