DataGridView broken after 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);

}

}
}
 
Back
Top