none
C Sharp:DragDrop from one dataGridView in Form A to another dataGridView in Form B RRS feed

  • Question

  • I got a problem : there are two diff form with a datagridview control, and i want to implement the drag and drop between the both grids to remove or add a data item. i spent a lot of time to find a demo on line, but the most of the are about the listbox but datagridview, so i ask a favor here! may be a demo ,a method tip, or a useful link, pls tell me! pls using the C Sharp program language. thx.
    Friday, April 23, 2010 2:55 AM

Answers

  • Hi aCooler,

    So, based on the fact that you can do the "dragdrop event" while the dataGridViews in the same form, the remainly work is to transfer data from one form to the destination form.

    Following demo shows how to transfer data from a textbox in FrmSub to a DataGridView in the destination form FrmMain:

    in the FrmSub.cs:

        public partial class FrmSub : Form
        {
            public FrmMain mainForm = null;

            public FrmSub()
            {
                InitializeComponent();
            }

            private void OKButton1_Click(object sender, EventArgs e)
            {
                mainForm.varTrans = this.textBox1.Text;
            }
        }

     

    in the FrmMain.cs:

        public partial class FrmMain : Form
        {
            private string _varTrans = string.Empty;
            public string varTrans
            {
                get
                {
                    return _varTrans;
                }
                set
                {
                    _varTrans = value;

                    dataGridView1.Rows.Add(_varTrans);
                }
            }

            public FrmMain()
            {
                InitializeComponent();
            }

            private void FrmMain_Shown(object sender, EventArgs e)
            {
                FrmSub frm = new FrmSub();
                frm.mainForm = this;
                frm.Show();
                frm.Activate();   
            }
        }

    You can adjust the code into transfer between two DataGridViews.
    Hope this helps.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Friday, April 30, 2010 2:38 AM
    Monday, April 26, 2010 7:51 AM
  • Not sure whether I have got your requirement correclt and I am a newby to this also. If you have 2 forms with datagridviews in each and you want to drag a row from Form 1 datagrid and drop it in From2 datagrid , the below method worked for me

    I have binded the datagridview using a bindingsource in each of the Forms. Both the Bindingsources datasources is essentially same Customer object.

    For the Form1 (Source DataGridView) - I have captured the Mouse down event and on RightMosueClick I am performing the drag operation

    private void sourceDGV_MouseDown(object sender, MouseEventArgs e)

    {

             if (e.Button == MouseButtons.Right)

            {

     

                   DataGridView.HitTestInfo info = sourceDGV.HitTest(e.X, e.Y);

     

                   if (info != null && info.RowIndex != -1)

                         {

     

                               Customer customerItem = (Customer)sourceDGV.Rows[info.RowIndex].DataBoundItem;

     

                                 if (customerItem != null)

                                  {

                                     sourceDGV.DoDragDrop(customerItem,

    DragDropEffects.Copy);

                                  }

                       }

               }

       }

     In Form2 (Destination DataGridView) have a event handler for the Drag Enter event

     

    private void destinationDGV_DragEnter(object sender, DragEventArgs e)

    {

     

       if (e.AllowedEffect == DragDropEffects.Copy)

       {

     

             Customer customer = e.Data.GetData(typeof(Customer)) as Customer;

     

             if (customer != null)

             {

                 _subCustomerList.Add(customer);

     

                this.subCustomerBindingSource.DataSource = _subCustomerList;

     

               this.subCustomerBindingSource.CurrencyManager.Refresh();       

           }

        }

     }

     

     

    • Marked as answer by Helen Zhou Friday, April 30, 2010 2:38 AM
    Wednesday, April 28, 2010 1:05 PM

All replies

  • can you please explain more , you want to drag and drop between both the grids ?????

    you mean u want to pass the values between 2 forms or something else


    if the post helpful please mark it right else feel free to correct me
    Saturday, April 24, 2010 5:04 PM
  • OK, i want to transfer some values form FormA to FormB by draging from the dataGridView in FormA to the one in the FormB.

    The dataGridView in FormA is a kind of datasource, and the dataGridView in FormB is the destination.

    the both dataGridView has the same column structure.

    Is that clear?

    I can do this "dragdrop event" while the dataGridViews in the same form, but not between the different forms which is the exact requirement.

    so, Could you do me a favor?

    Saturday, April 24, 2010 6:26 PM
  • Hi aCooler,

    So, based on the fact that you can do the "dragdrop event" while the dataGridViews in the same form, the remainly work is to transfer data from one form to the destination form.

    Following demo shows how to transfer data from a textbox in FrmSub to a DataGridView in the destination form FrmMain:

    in the FrmSub.cs:

        public partial class FrmSub : Form
        {
            public FrmMain mainForm = null;

            public FrmSub()
            {
                InitializeComponent();
            }

            private void OKButton1_Click(object sender, EventArgs e)
            {
                mainForm.varTrans = this.textBox1.Text;
            }
        }

     

    in the FrmMain.cs:

        public partial class FrmMain : Form
        {
            private string _varTrans = string.Empty;
            public string varTrans
            {
                get
                {
                    return _varTrans;
                }
                set
                {
                    _varTrans = value;

                    dataGridView1.Rows.Add(_varTrans);
                }
            }

            public FrmMain()
            {
                InitializeComponent();
            }

            private void FrmMain_Shown(object sender, EventArgs e)
            {
                FrmSub frm = new FrmSub();
                frm.mainForm = this;
                frm.Show();
                frm.Activate();   
            }
        }

    You can adjust the code into transfer between two DataGridViews.
    Hope this helps.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Friday, April 30, 2010 2:38 AM
    Monday, April 26, 2010 7:51 AM
  • Not sure whether I have got your requirement correclt and I am a newby to this also. If you have 2 forms with datagridviews in each and you want to drag a row from Form 1 datagrid and drop it in From2 datagrid , the below method worked for me

    I have binded the datagridview using a bindingsource in each of the Forms. Both the Bindingsources datasources is essentially same Customer object.

    For the Form1 (Source DataGridView) - I have captured the Mouse down event and on RightMosueClick I am performing the drag operation

    private void sourceDGV_MouseDown(object sender, MouseEventArgs e)

    {

             if (e.Button == MouseButtons.Right)

            {

     

                   DataGridView.HitTestInfo info = sourceDGV.HitTest(e.X, e.Y);

     

                   if (info != null && info.RowIndex != -1)

                         {

     

                               Customer customerItem = (Customer)sourceDGV.Rows[info.RowIndex].DataBoundItem;

     

                                 if (customerItem != null)

                                  {

                                     sourceDGV.DoDragDrop(customerItem,

    DragDropEffects.Copy);

                                  }

                       }

               }

       }

     In Form2 (Destination DataGridView) have a event handler for the Drag Enter event

     

    private void destinationDGV_DragEnter(object sender, DragEventArgs e)

    {

     

       if (e.AllowedEffect == DragDropEffects.Copy)

       {

     

             Customer customer = e.Data.GetData(typeof(Customer)) as Customer;

     

             if (customer != null)

             {

                 _subCustomerList.Add(customer);

     

                this.subCustomerBindingSource.DataSource = _subCustomerList;

     

               this.subCustomerBindingSource.CurrencyManager.Refresh();       

           }

        }

     }

     

     

    • Marked as answer by Helen Zhou Friday, April 30, 2010 2:38 AM
    Wednesday, April 28, 2010 1:05 PM