PC Review


Reply
Thread Tools Rate Thread

DataGridView broken after drag drop

 
 
Mike Oliszewski
Guest
Posts: n/a
 
      22nd Jan 2009

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);

}

}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Drag and Drop in DataGridView? Patrick A Microsoft VB .NET 0 23rd Mar 2010 02:00 PM
Drag Drop Onto DataGridView joey.powell@topscene.com Microsoft C# .NET 0 12th Jul 2006 08:29 PM
DataGridView Drag And Drop =?Utf-8?B?U2F0Q29t?= Microsoft Dot NET Framework Forms 0 22nd May 2006 07:34 AM
DATAGRIDVIEW DRAG AND DROP =?Utf-8?B?U2F0Q29t?= Microsoft Dot NET Framework Forms 0 22nd May 2006 07:17 AM
Drag and Drop with DataGridView =?Utf-8?B?Y25pY2ts?= Microsoft Dot NET Framework Forms 3 28th Mar 2006 04:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:45 AM.