如何在C#中的两个数据网格之间拖放?

时间:2019-07-05 19:48:51

标签: c# datagridview

我在两个数据网格之间的拖放存在很多问题;我可以获取第一个的数据,但不能将其添加到另一个。

我进行了很多研究,但是所有解决方案都是针对Visual Basic的,我也尝试使用文本框和列表框来做到这一点,并且我可以做到,但是现在我只需要使用datagrids来实现

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ListBox1.AllowDrop = true;
        List<MyItem> data = new List<MyItem>() { new MyItem("ola"), new MyItem("k"), new MyItem("ase") };
        dtgConfigList.ItemsSource = data;
        dtgConfigList.Items.Refresh();
        dtgEditConfig.Items.Refresh();

    }

    private void txtOla_MouseDown(object sender, MouseButtonEventArgs e)
    {
        txtOla.Focus();
        txtOla.SelectAll();
        var dataObj = new DataObject(ListBox1);
        dataObj.SetData("DragSource", txtOla);
        DragDrop.DoDragDrop(txtOla, dataObj, DragDropEffects.Copy);
    }

    private void OnDrop(object sender, DragEventArgs e)
    {

        ListBox1.Items.Add(txtOla.Text);
        txtOla.Clear();
    }

    private void dtgConfigListMouseDown(object sender, MouseButtonEventArgs e)
    {
        var dataObj = new DataObject(dtgEditConfig);
        DragDrop.DoDragDrop(dtgConfigList,dataObj,DragDropEffects.Copy);

    }

    private void DtgConfigList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void dtgEditDrop(object sender, DragEventArgs e)
    {


        DataTable dt = new DataTable();

        Point clientPoint;
        clientPoint = dtgEditConfig.PointToScreen(new Point());
        DataGridRow asd = dtgEditConfig.InputHitTest(clientPoint);

        if (e.Effects == DragDropEffects.Move)
        {


            dt = this.dtgEditConfig.DataSource as DataTable;

            DataRow dr = dt.NewRow();

            dr[0] = rowToMove.Cells[0].Value.ToString();

            dr[1] = rowToMove.Cells[1].Value.ToString();

            dt.Rows.InsertAt(dr, rowIndexTo);
        }
    }

    private void dtgEditEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(System.String)))
            e.Effects = DragDropEffects.Copy;
        else
            e.Effects = DragDropEffects.None;

        //dtgEditConfig.Items.Add(dtgEditConfig);
    }
}

0 个答案:

没有答案