C#vs的windows窗体的datagridview之间数据的拖拽

C#vs的windows窗体的datagridview之间数据的拖拽

效果

datagridview数据拖拽

代码

1.新建对象:

private DataGridViewSelectedRowCollection sourceRowCollection = null;

2.在dataGridView1(上表:药品库存单)的按下鼠标事件(dataGridView1_MouseDown)中编辑:

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            //捕获鼠标点击区域的信息
            DataGridView.HitTestInfo hitTestInfo = this.dataGridView1.HitTest(e.X, e.Y);

            if (e.X < 30 && hitTestInfo.RowIndex > -1)
            {
                if (this.dataGridView1.SelectedRows.Count > 0)
                {
                    sourceRowCollection = this.dataGridView1.SelectedRows;
                }
            }
            else
                sourceRowCollection = null;
        }

3.在dataGridView1(上表:药品库存单)的鼠标移动事件(dataGridView1_MouseMove)中编辑:

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (sourceRowCollection != null)
                {
                    DragDropEffects effect = this.dataGridView1.DoDragDrop(sourceRowCollection, DragDropEffects.Move);
                    if (effect == DragDropEffects.Move)
                    {
                        //将sourceRowCollection重新置空
                        sourceRowCollection = null;
                    }
                }
            }
        }

4.在dataGridView2(下表:处方单)的将对象拖过控件事件(dataGridView2_DragOver)中编辑:

private void dataGridView2_DragOver(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
            {

                e.Effect = DragDropEffects.None;
                return;
            }
            else
            {
                e.Effect = DragDropEffects.Move;  //这个值会返回给DoDragDrop方法
            }
        }

5.在dataGridView2(下表:处方单)的拖放操作完成事件(dataGridView2_DragDrop)中编辑:

private void dataGridView2_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
                {
                    DataGridViewSelectedRowCollection rowCollection = e.Data.GetData(typeof(DataGridViewSelectedRowCollection)) as DataGridViewSelectedRowCollection;
                    if (rowCollection == null)
                    {
                        return;
                    }
                    //新增行
                    //注意要将鼠标的Point转换到当前工作区域,否则无法得到正确的HitTestInfo
                    System.Drawing.Point p = this.dataGridView2.PointToClient(new System.Drawing.Point(e.X, e.Y));
                    DataGridView.HitTestInfo hitTestInfo = this.dataGridView2.HitTest(p.X, p.Y);
                    //如果鼠标所在的位置的RowIndex>-1,则在当前位置接入列,否则就在最末尾新增列
                    if (hitTestInfo.RowIndex > -1)
                    {
                        this.dataGridView2.Rows.Insert(hitTestInfo.RowIndex + 1, rowCollection.Count);
                        for (int i = 0; i < rowCollection.Count; i++)
                        {
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[0].Value = rowCollection[i].Cells[2].Value;//药品
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[1].Value = rowCollection[i].Cells[3].Value;//剂型
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[2].Value = rowCollection[i].Cells[4].Value;//规格
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[3].Value = rowCollection[i].Cells[5].Value;//单位
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[4].Value = rowCollection[i].Cells[6].Value;//产地
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[5].Value = rowCollection[i].Cells[7].Value;//单价
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[6].Value = rowCollection[i].Cells[9].Value;//有效日期
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[7].Value = 0;//数量
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[8].Value = 0;//金额
                            double text7 = 0;
                            textBox7.Text = text7.ToString();//这个textBox7是“总计”,拖入药品输入数量,总计会相应变化
                        }
                    }
                    else
                    {
                        foreach (DataGridViewRow row in rowCollection)
                        {
                            int i = this.dataGridView2.Rows.Add();
                            this.dataGridView2.Rows[i].Cells[0].Value = row.Cells[2].Value;
                            this.dataGridView2.Rows[i].Cells[1].Value = row.Cells[3].Value;
                            this.dataGridView2.Rows[i].Cells[2].Value = row.Cells[4].Value;
                            this.dataGridView2.Rows[i].Cells[3].Value = row.Cells[5].Value;
                            this.dataGridView2.Rows[i].Cells[4].Value = row.Cells[6].Value;
                            this.dataGridView2.Rows[i].Cells[5].Value = row.Cells[7].Value;
                            this.dataGridView2.Rows[i].Cells[6].Value = row.Cells[9].Value;
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[7].Value = 0;
                            this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[8].Value = 0;
                            double text7 = 0;
                            textBox7.Text = text7.ToString();//这个textBox7是“总计”,拖入药品输入数量,总计会相应变化
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

6.完成。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值