【问题标题】:Drag&Drop: DataGridView rows "between" TabPage拖放:DataGridView 行“之间”TabPage
【发布时间】:2014-02-16 18:23:36
【问题描述】:

[已解决] 我想将一些 DataGridViewRows 从包含在 TabPage 中的 DataGridView 拖到也包含在另一个 TabPage 中的另一个 DataGridView 中。我已经设置了 DataGridView 的事件 (How could I Drag and Drop DataGridView Rows under each other?),但我不知道如何在 TabPages 之间“导航”!

【问题讨论】:

    标签: c# winforms datagridview drag-and-drop tabcontrol


    【解决方案1】:

    这是一个简单的示例,如何将文本从选项卡上的一个文本框拖到单独选项卡上的另一个文本框中:

    private void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
        textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy | DragDropEffects.Move);
    }
    
    private void tabControl1_DragOver(object sender, DragEventArgs e)
    {
        Point location = tabControl1.PointToClient(Control.MousePosition);
        for (int tab = 0; tab < tabControl1.TabCount; ++tab)
        {
            if (tabControl1.GetTabRect(tab).Contains(location))
            {
                tabControl1.SelectedIndex = tab;
                break;
            }
        }
    }
    
    private void textBox2_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }
    
    private void textBox2_DragDrop(object sender, DragEventArgs e)
    {
        textBox2.Text = e.Data.GetData(DataFormats.Text).ToString();
    }
    

    注意:您必须在 TabControl 上将 AllowDrop 属性设置为 true,当然在目标控件上也是如此。

    干杯

    【讨论】:

      【解决方案2】:

      我自己(和@mrlucmorin)解决了这个问题:

      internal void dgv_MouseDown(object sender, MouseEventArgs e)
      {
           DataGridView dgv = (DataGridView)sender;
           List<DataGridViewRow> result = new List<DataGridViewRow>();
           foreach(DataGridViewRow row in dgv.SelectedRows)
           {
               result.Add(row);
           }
           dgv.DoDragDrop(result, DragDropEffects.Copy | DragDropEffects.Move);
      }
      
      private void dgv_DragEnter(object sender, DragEventArgs e)
      {
           e.Effect = DragDropEffects.Copy;
      }
      
      private void dgv_DragDrop(object sender, DragEventArgs e)
      {
           try
           {
                DataGridView dataGridView1 = (DataGridView)sender;
                List<DataGridViewRow> rows = new List<DataGridViewRow>();
                rows = (List<DataGridViewRow>)e.Data.GetData(rows.GetType());
      
                //some stuff
           }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-08
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多