DoubleClick broken by drag drop

  • Thread starter Thread starter Mike Oliszewski
  • Start date Start date
M

Mike Oliszewski

Problem: The first mouse click on a DataGridView after performing a drag
drop operation is ignored.

The application below demostrates the problem we are having. It nicely
allows me to grab an item from the DataGridView and drop it into the
ListView. The problem we are encountering however is that the next mouse
click performed on the DataGridView is ignored... making is necessary to
click twice in order to change the selected element.

Is there some mechanism or operation I need to perform at the time of the
drop to cancel whatever state the DataGridView is staying in ?


//*** Application demonstrating drag drop problem *****

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

namespace DragDropTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

public class Form1 : Form
{
private System.Windows.Forms.DataGridView ITEMS;
private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
private System.Windows.Forms.ListView CATEGORIES;

private void Form1_Load(object sender, EventArgs e)
{
ITEMS.Rows.Add(new object[] { "item 1" });
ITEMS.Rows.Add(new object[] { "item 2" });
ITEMS.Rows.Add(new object[] { "item 3" });
}

private void ITEMS_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = ITEMS.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
ITEMS.DoDragDrop(ITEMS.Rows[info.RowIndex].Cells[0].Value,
DragDropEffects.All);
}

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

private void CATEGORIES_DragDrop(object sender, DragEventArgs e)
{
System.Diagnostics.Trace.WriteLine("droped: " +
e.Data.GetData(typeof(string)).ToString());
}

public Form1()
{
System.Windows.Forms.ListViewItem listViewItem1 = new
System.Windows.Forms.ListViewItem("Category 1");
this.ITEMS = new System.Windows.Forms.DataGridView();
this.Column1 = new
System.Windows.Forms.DataGridViewTextBoxColumn();
this.CATEGORIES = new System.Windows.Forms.ListView();

((System.ComponentModel.ISupportInitialize)(this.ITEMS)).BeginInit();
this.SuspendLayout();
//
// ITEMS
//
this.ITEMS.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.ITEMS.Columns.AddRange(new
System.Windows.Forms.DataGridViewColumn[] {
this.Column1});
this.ITEMS.Location = new System.Drawing.Point(195, 25);
this.ITEMS.Name = "ITEMS";
this.ITEMS.Size = new System.Drawing.Size(240, 97);
this.ITEMS.TabIndex = 0;
this.ITEMS.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.ITEMS_MouseDown);
//
// Column1
//
this.Column1.HeaderText = "Column1";
this.Column1.Name = "Column1";
//
// CATEGORIES
//
this.CATEGORIES.AllowDrop = true;
this.CATEGORIES.Items.AddRange(new
System.Windows.Forms.ListViewItem[] {
listViewItem1});
this.CATEGORIES.Location = new System.Drawing.Point(12, 25);
this.CATEGORIES.Name = "CATEGORIES";
this.CATEGORIES.Size = new System.Drawing.Size(144, 97);
this.CATEGORIES.TabIndex = 1;
this.CATEGORIES.UseCompatibleStateImageBehavior = false;
this.CATEGORIES.View = System.Windows.Forms.View.List;
this.CATEGORIES.DragDrop += new
System.Windows.Forms.DragEventHandler(this.CATEGORIES_DragDrop);
this.CATEGORIES.DragEnter += new
System.Windows.Forms.DragEventHandler(this.CATEGORIES_DragEnter);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(447, 156);
this.Controls.Add(this.CATEGORIES);
this.Controls.Add(this.ITEMS);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.DragEnter += new
System.Windows.Forms.DragEventHandler(this.CATEGORIES_DragEnter);

((System.ComponentModel.ISupportInitialize)(this.ITEMS)).EndInit();
this.ResumeLayout(false);

}

}
}
 
Hi mikeoliszewski,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

It seems that when performing the drag and drop operation, the DataGridView
is in a state that prevent it from setting the current cell to the one that
user clicks. To workaround this problem, you can set the CurrentCell
property for the DataGridView control manually. The following code works:

private void ITEMS_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = ITEMS.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
ITEMS.DoDragDrop(ITEMS.Rows[info.RowIndex].Cells[1].Value,
DragDropEffects.All);

//Set the CurrentCell property manually.
this.ITEMS.CurrentCell = this.ITEMS[0, info.RowIndex];
}
}

If you have any questions or concerns, please feel free to let me know.

Have a great day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top