DataGridView鼠标的拖放操作及单元格编辑结束数据复制与单元格单击数据粘贴及两个DataGridView相互拖放操作

昨天观世音菩萨出家纪念日,昨晚上本想发布的,一忙其他事情给忘了,今早补上,以资纪念.2013年10月23日:二〇一三年九月十九

DataGridView鼠标的拖放操作及单元格编辑结束数据复制与单元格单击数据粘贴及两个DataGridView相互拖放操作
        private void DataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = (e.Data.GetDataPresent(typeof(System.String)) && DataGridView1.Rows.Count > 0)
                ? DragDropEffects.Copy : DragDropEffects.None;
        }

        private void DataGridView1_DragDrop(object sender, DragEventArgs e)
        {

                if (e.Data.GetDataPresent(typeof(System.String)) && DataGridView1.Rows.Count > 0)
                {
                    Point 二维面 = DataGridView1.PointToClient(new Point(e.X, e.Y));
                    DataGridView1.Rows[DataGridView1.HitTest(二维面.X, 二维面.Y).RowIndex].Cells[DataGridView1.HitTest(二维面.X, 二维面.Y).ColumnIndex].Value = (System.String)e.Data.GetData(typeof(System.String));
                }
        }

        private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
                string 单元值 = "";
                if (DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                    单元值 = DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                DataGridView1.DoDragDrop(单元值, DragDropEffects.Copy);
        }

        private void DataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridView2.DoDragDrop(DataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), DragDropEffects.Copy);
        }

        private void DataGridView2_DragDrop(object sender, DragEventArgs e)
        {
                if (e.Data.GetDataPresent(typeof(System.String)) && DataGridView2.Rows.Count > 0)
                {
                    Point 二维面 = DataGridView2.PointToClient(new Point(e.X, e.Y));
                        DataGridView2.Rows[DataGridView2.HitTest(二维面.X, 二维面.Y).RowIndex].Cells[DataGridView2.HitTest(二维面.X, 二维面.Y).ColumnIndex].Value = (System.String)e.Data.GetData(typeof(System.String));
                }
        }

        private void DataGridView2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = (e.Data.GetDataPresent(typeof(System.String)))
                ? DragDropEffects.Copy : DragDropEffects.None;
        }

        private void DataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
                string 单元值 = DataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                DataGridView2.DoDragDrop(单元值, DragDropEffects.Copy);
        }
最后顺便说一下,如果richTextBox控件的属性EeableAutoDragDrop设置为True则可以拖放数据到DataGridView中。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设有一个 DataGridView 名为 dgv1,其中有多个单元格选中。现在需要将这些选中单元格中的数据复制到其他选中单元格中。以下是一个详细的实现过程: 1. 在 Form_Load 事件中添加以下代码,启用 DataGridView 的多选模式: ``` dgv1.MultiSelect = True dgv1.SelectionMode = DataGridViewSelectionMode.CellSelect ``` 2. 在 DataGridView 的 CellMouseDown 事件中添加以下代码,用于记录下选中单元格的位置信息: ``` Private selectedCells As New List(Of DataGridViewCell) Private Sub dgv1_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgv1.CellMouseDown If e.Button = MouseButtons.Left AndAlso e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then selectedCells.Clear() For Each cell As DataGridViewCell In dgv1.SelectedCells selectedCells.Add(cell) Next selectedCells.Add(dgv1(e.ColumnIndex, e.RowIndex)) End If End Sub ``` 3. 在 DataGridView 的 CellMouseUp 事件中添加以下代码,用于将选中单元格中的数据复制到其他选中单元格中: ``` Private Sub dgv1_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgv1.CellMouseUp If e.Button = MouseButtons.Left AndAlso e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 AndAlso selectedCells.Count > 1 Then Dim sourceCells(selectedCells.Count - 1) As String Dim sourceIndex As Integer = 0 For Each cell As DataGridViewCell In selectedCells sourceCells(sourceIndex) = cell.Value.ToString() sourceIndex += 1 Next Dim destCells As New List(Of DataGridViewCell) For Each cell As DataGridViewCell In dgv1.SelectedCells If Not selectedCells.Contains(cell) Then destCells.Add(cell) End If Next If sourceCells.Length = destCells.Count Then Dim destIndex As Integer = 0 For Each cell As DataGridViewCell In destCells cell.Value = sourceCells(destIndex) destIndex += 1 Next Else MessageBox.Show("The number of selected source cells doesn't match the number of selected destination cells.") End If End If End Sub ``` 以上代码中,我们首先判断是否按下了左键并且选中了至少两个单元格,然后获取当前鼠标点击的单元格作为源单元格。然后将选中的源单元格中的数据存储到一个字符串数组 sourceCells 中。接下来,遍历 DataGridView 中除了选中的源单元格之外的其他选中单元格,并将其添加到 destCells 列表中。然后判断源单元格的数量是否和目标单元格的数量相等,如果相等,则将源单元格数据复制到目标单元格中,否则弹出一个提示框。 这样就实现了从 DataGridView 选定的多个单元格数据复制到其他选中单元格中的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值