CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2002
    Posts
    17

    DragDrop in DataGrid

    Can someone tell me how to Drop something in DataGrid.

    private void Interaction_dgr_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
    DataGrid.HitTestInfo hti = Interaction_dgr.HitTest(new Point(e.X, e.Y));

    MessageBox.Show(hti.Column.ToString());
    }
    My HitTestInfo object return -1 when i'm trying to get Row and Column properties.

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    i think we both are on same track. i am doing the same thing.

    what i did is like this.

    1) this.dataGridAppGrid.AllowDrop = true;
    this.dataGridAppGrid.DragEnter += new System.Windows.Forms.DragEventHandler(this.dataGridAppGrid_DragEnter);
    this.dataGridAppGrid.DragDrop += new System.Windows.Forms.DragEventHandler(this.dataGridAppGrid_DragDrop);

    implement dragover also if u want.

    private void dataGridAppGrid_DragEnter(object sender, DragEventArgs e)
    {
    if ( e.Data.GetDataPresent("AptGrid.AppointmentLabel") )
    e.Effect = DragDropEffects.All;
    }

    you need to check with whole namespace if ur draged item is a user control..

    hope this helps a bit.

    Paresh

  3. #3
    Join Date
    Apr 2002
    Posts
    17
    Thank's for replay Pareshgh, but it don't help me .
    I'm trying to grag a value from treeview control and drop it in DataGrig control , I have a right e.X and e.Y value of event , but when I make HitTrstInfo object Row and Column properties returns -1 .I realy do not understand what is the problem.

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    when u drag an item from tree then u need to set the object and when u get the drag recived in datagrid. then u need to convert back in proper format.
    ..

    could you post ur source code .. may be something can be figured out.

    Paresh

  5. #5
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    did you got some work around ?

    Paresh

  6. #6
    Join Date
    Apr 2002
    Posts
    17
    Hi .

    I'll be happy if someone can told me where is my mistake.
    This is my code:

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace testWin
    {

    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.TreeView treeView1;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.DataGrid dataGrid1;
    private DataTable o_DataTable;

    private System.ComponentModel.Container components = null;

    public Form1()
    {
    InitializeComponent();
    DataBind();
    }

    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code

    private void InitializeComponent()
    {
    this.panel1 = new System.Windows.Forms.Panel();
    this.treeView1 = new System.Windows.Forms.TreeView();
    this.panel2 = new System.Windows.Forms.Panel();
    this.dataGrid1 = new System.Windows.Forms.DataGrid();
    this.panel1.SuspendLayout();
    this.panel2.SuspendLayout();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
    this.SuspendLayout();
    //
    // panel1
    //
    this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.treeView1});
    this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(120, 273);
    this.panel1.TabIndex = 0;
    //
    // treeView1
    //
    this.treeView1.AllowDrop = true;
    this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.treeView1.ImageIndex = -1;
    this.treeView1.Name = "treeView1";
    this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
    new System.Windows.Forms.TreeNode("Node0", new System.Windows.Forms.TreeNode[] {
    new System.Windows.Forms.TreeNode("Node1"),
    new System.Windows.Forms.TreeNode("Node3"),
    new System.Windows.Forms.TreeNode("Node4"),
    new System.Windows.Forms.TreeNode("Node5")})});
    this.treeView1.SelectedImageIndex = -1;
    this.treeView1.Size = new System.Drawing.Size(120, 273);
    this.treeView1.TabIndex = 0;
    this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView1_DragEnter);
    this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
    //
    // panel2
    //
    this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.dataGrid1});
    this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
    this.panel2.Location = new System.Drawing.Point(120, 0);
    this.panel2.Name = "panel2";
    this.panel2.Size = new System.Drawing.Size(368, 273);
    this.panel2.TabIndex = 1;
    //
    // dataGrid1
    //
    this.dataGrid1.AllowDrop = true;
    this.dataGrid1.DataMember = "";
    this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
    this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid1.Name = "dataGrid1";
    this.dataGrid1.Size = new System.Drawing.Size(368, 273);
    this.dataGrid1.TabIndex = 0;
    this.dataGrid1.DragDrop += new System.Windows.Forms.DragEventHandler(this.dataGrid1_DragDrop);
    this.dataGrid1.DragEnter += new System.Windows.Forms.DragEventHandler(this.dataGrid1_DragEnter);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(488, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.panel2,
    this.panel1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.panel1.ResumeLayout(false);
    this.panel2.ResumeLayout(false);
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
    this.ResumeLayout(false);

    }
    #endregion


    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void DataBind()
    {
    o_DataTable = new DataTable("Data");
    DataColumn o_DataColumn = new DataColumn("Col1",typeof(string));
    o_DataTable.Columns.Add(o_DataColumn);

    o_DataColumn = new DataColumn("Col2",typeof(string));
    o_DataTable.Columns.Add(o_DataColumn);

    DataRow o_DataRow = o_DataTable.NewRow();
    o_DataRow[0] = "A";
    o_DataRow[1] = "B";
    o_DataTable.Rows.Add(o_DataRow);

    this.dataGrid1.DataSource = o_DataTable;


    }

    private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
    {
    if (e.Data.GetDataPresent(DataFormats.Text))
    e.Effect = DragDropEffects.Copy;
    else
    e.Effect = DragDropEffects.None;
    }

    private void treeView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
    {
    string strItem = e.Item.ToString();
    DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);

    }

    private void dataGrid1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
    {
    if ( e.Data.GetDataPresent(DataFormats.Text) )
    e.Effect = DragDropEffects.Copy;

    }

    private void dataGrid1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
    if ( e.Data.GetDataPresent(DataFormats.Text) )
    {
    e.Effect = DragDropEffects.Copy;
    DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(new Point(e.X, e.Y));
    MessageBox.Show(hti.Column.ToString());
    }
    }
    }
    }

  7. #7
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890

    modify your drag-drop in datagrid

    hi,
    modify your drag-drop like following and test again.

    private void dataGrid1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {
    if ( e.Data.GetDataPresent(DataFormats.Text) )
    {
    e.Effect = DragDropEffects.Copy;
    Point pt = new Point(e.X,e.Y);
    pt = dataGrid1.PointToClient(pt);
    DataGrid.HitTestInfo hti = this.dataGrid1.HitTest(pt);
    MessageBox.Show(hti.Column.ToString());
    }
    }


    Hope this should work
    -Paresh

  8. #8
    Join Date
    Apr 2002
    Posts
    17
    Thank's, Pareshgh, it work!!!!

  9. #9
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    i m glad for u

  10. #10
    Join Date
    Jun 2008
    Posts
    1

    Re: DragDrop in DataGrid

    I am a new member I am facing the same problem I just want to see weather I will get reply I am mailing you guys.If you can help me say so from there We can start Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured