Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,081 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




drag and drop in dataGridView

 
Reply to this topicStart new topic

drag and drop in dataGridView

liviu
4 Apr, 2008 - 09:21 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 3

Hello c# coders,
I have 2 data grid views (dataGridView1 and dataGridView2) that brings data from an sql database. In dataGridView1 it is an empty collumn, named C3. In dataGridView2 i have 2 collumns C1 and C2. I need to do drag and drop from column C1 in dataGridView2 to C3 column in dataGridView1. I spend half of day on google but i can't find something useful. Advance thanks for any answer.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Drag And Drop In DataGridView
4 Apr, 2008 - 06:40 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Use the DragDrop event of the DataGridView
User is offlineProfile CardPM
+Quote Post

liviu
RE: Drag And Drop In DataGridView
6 Apr, 2008 - 10:21 AM
Post #3

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 3

i have verry good results, but there is something that seems not to work. I have dataGridView1 from witch i want to drag data and drop to dataGridView2. Looks Ok for dataGridView1, but when i tryed to drop data to dataGridView2 the content is not send. where am I wrong?
CODE

private void dataGridView2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(System.String)))
            {
                DataGridView.HitTestInfo info2 = dataGridView2.HitTest(e.X, e.Y);
                if (info2.RowIndex >= 0)
                {
                    if (info2.RowIndex >= 0 && info2.ColumnIndex >= 0)
                    {
                        string baluba = (System.String)e.Data.GetData(typeof(System.String));
                        if (baluba != null)
                        
                        {
                            dataGridView2.CurrentCell.Value = baluba;
                            
                            // dataGridView2.Rows[info2.RowIndex].Cells[info2.ColumnIndex].Value = baluba; --also tryed
                            //dataGridView2.DoDragDrop(baluba, DragDropEffects.Move);


                        }
                    }
                }

            }
        }


private void dataGridView2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }




and off course in Designer.cs i have:
CODE

this.dataGridView2.AllowDrop = true;
            this.dataGridView2.AutoGenerateColumns = false;
            this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.sUPLNODataGridViewTextBoxColumn,
            this.sKEYDataGridViewTextBoxColumn,
            this.C3,
            this.C4});
            this.dataGridView2.DataSource = this.sUPLBindingSource;
            this.dataGridView2.Location = new System.Drawing.Point(16, 13);
            this.dataGridView2.MultiSelect = false;
            this.dataGridView2.Name = "dataGridView2";
            this.dataGridView2.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            this.dataGridView2.Size = new System.Drawing.Size(444, 150);
            this.dataGridView2.TabIndex = 5;
            this.dataGridView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragEnter);
            this.dataGridView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragDrop);
            this.dataGridView2.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellContentClick);
            

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Drag And Drop In DataGridView
6 Apr, 2008 - 03:41 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Inside the DragEnter event of the DataGridView that you are dropping the content into. You need this line of code to actually cause the move to take effect. Don't forget to make sure the AllowDrop property is set to True of the control you will be dropping onto.
CODE

e.Effect = DragDropEffects.Copy;

User is offlineProfile CardPM
+Quote Post

liviu
RE: Drag And Drop In DataGridView
7 Apr, 2008 - 09:54 PM
Post #5

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 3

thanks a lot for your answers
It is working if I put like:
CODE

private void dataGridView2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(System.String)))
{
               string baluba = (System.String)e.Data.GetData(typeof(System.String));
               dataGridView2.CurrentCell.Value = baluba;
}
}
                           //and

private void dataGridView2_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
          
        }



but i don't want for the current cell, i want for the cell that i am with my mouse over and release the left click.maybe something wrong in HitTest?
It is working with
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:42PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month