Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - dataGridView CellContentClick fires after CellDoubleClick

Reply
 
Old 02-03-2009   #1 (permalink)
QSIDeveloper


 
 

dataGridView CellContentClick fires after CellDoubleClick

I have a bound DataGridView control and in the CellDoubleClick event of a
column set as DataGridLinkColumn I add content to the underlying databound
item of a new row. If the text that is added is long enough to be in the
coordinates where the user double clicked the CellContentClick fires after
the CellDoubleClick event returns. Is this expected behavior? Is there a way
to prevent the CellContentClick from firing after content is added? I am
using .NET 2.0

My System SpecsSystem Spec
Old 02-04-2009   #2 (permalink)
Zhi-Xin Ye [MSFT]


 
 

RE: dataGridView CellContentClick fires after CellDoubleClick

Hi,

Thank you for using Microsoft Managed Newsgroup Service, it's my pleasure
to work with you on this issue.

As I understand, you have a DataGridView control which is data bound, one
of its columns is a DataGridViewLinkColumn. And you handle the
CellDoubleClick event, in the event handler, you add content for the
current row, and you found if the content in the DataGridViewLinkCell is
long enough to be in the place where the user double clicks, the
CellContentClick event fires as well.

I try to reproduce this problem with the following code, when I double
click on the DataGridViewLinkCell, the CellDoubleClick event fires and
content is added to the cell, but the CellContentClick event does not fire.

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("c1", typeof(int));
dt.Columns.Add("c2");
for (int j = 0; j < 50; j++)
{
dt.Rows.Add(j, "");
}

this.dataGridView1.DataSource = dt;

DataGridViewLinkColumn linkCol = new DataGridViewLinkColumn();
linkCol.DataPropertyName = "c2";
this.dataGridView1.Columns.Add(linkCol);

this.dataGridView1.CellDoubleClick += new
DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
this.dataGridView1.CellContentClick += new
DataGridViewCellEventHandler(dataGridView1_CellContentClick);
}

void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
Console.WriteLine("Cell Content");
}

void dataGridView1_CellDoubleClick(object sender,
DataGridViewCellEventArgs e)
{
Console.WriteLine("Cell double");

if (e.ColumnIndex == 2)
{
DataRowView drv =
this.dataGridView1.CurrentRow.DataBoundItem as DataRowView;
drv["c2"] =
"asccccccccccccccccccccccccccccccccccccccccccccc";
}
}

If I misunderstand you, please let me know. I look forward to your reply.

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:
msdnmg@xxxxxx.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Old 02-04-2009   #3 (permalink)
QSIDeveloper


 
 

RE: dataGridView CellContentClick fires after CellDoubleClick

Hi

Here is a sample of my code

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

namespace AutomaticStage
{
public partial class AutomaticVar : Form
{
public AutomaticVar()
{
DataTable trueFalse = new DataTable();
trueFalse.Columns.Add("DisplayMem",
System.Type.GetType("System.String"));
trueFalse.Columns.Add("ValueMem",
System.Type.GetType("System.Boolean"));
DataRow row = trueFalse.NewRow();
row[0] = "True";
row[1] = true;
trueFalse.Rows.Add(row);
row = trueFalse.NewRow();
row[0] = "False";
row[1] = false;
trueFalse.Rows.Add(row);


InitializeComponent();

stageDetailsBindingSource.Add(new StageDetails(true, "A EQ
InvoiceNumber AND InvoiceDate GT today", 1));
stageDetailsBindingSource.Add(new StageDetails(false, "C=D", 2));
stageDetailsBindingSource.Add(new StageDetails(true, "E=F", 3));
//stageDetailsBindingSource.Add(new StageDetails(false, "C=D",
2));
//stageDetailsBindingSource.Add(new StageDetails(true, "E=F", 3));
//stageDetailsBindingSource.Add(new StageDetails(false, "C=D",
2));
//stageDetailsBindingSource.Add(new StageDetails(true, "E=F", 3));
//stageDetailsBindingSource.Add(new StageDetails(false, "C=D",
2));
//stageDetailsBindingSource.Add(new StageDetails(true, "E=F", 3));
//stageDetailsBindingSource.Add(new StageDetails(false, "C=D",
2));
//stageDetailsBindingSource.Add(new StageDetails(true, "E=F", 3));

stageDetailsBindingSource.AllowNew = true;

conditionComboboxColumn.ValueMember = "ValueMem";
conditionComboboxColumn.DisplayMember = "DisplayMem";
conditionComboboxColumn.DataSource = trueFalse;

this.dataGridView1.CellDoubleClick += new
DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
this.dataGridView1.CellContentClick += new
DataGridViewCellEventHandler(dataGridView1_CellContentClick);
this.dataGridView1.MouseMove +=new
MouseEventHandler(dataGridView1_MouseMove);
this.dataGridView1.MouseDown +=new
MouseEventHandler(dataGridView1_MouseDown);
this.dataGridView1.DragDrop +=new
DragEventHandler(dataGridView1_DragDrop);
this.dataGridView1.DragOver +=new
DragEventHandler(dataGridView1_DragOver);


dataGridViewElse.Rows.Add("Else", "");
//dataGridViewElse.Rows.Add("hidden", "");
//dataGridViewElse.Rows[1].Selected = true;
}

void dataGridView1_CellDoubleClick(object sender,
DataGridViewCellEventArgs e)
{

Form frm = new ExpressionTiff(); // simulate expresson form
frm.ShowDialog();


int a = 0;
stageDetailsBindingSource.ResetBindings(false);
StageDetails stageDetail =
(StageDetails)this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].DataBoundItem;
stageDetail.Condition = true;
stageDetail.ExpressionText = "AAAAAAAAAAAAAAAAAAAAAAAAAAAA";//
"G=H qweqwuieyuiyeqwuyqweu qwkjbcsdjkfhweiug";
stageDetail.ExpressionIcode = 4;



//stageDetailsBindingSource.SuspendBinding();
//stageDetailsBindingSource.Add(new StageDetails(true, "G=H", 4));
//stageDetailsBindingSource.ResumeBinding();

//StageDetails[] details = stageDetailsBindingSource.DataSource
as StageDetails[];
//StageDetails[] details =
(StageDetails[])stageDetailsBindingSource.DataSource;

}

void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
if (IsANonHeaderButtonCell(e))
{
StageDetails stageDetail =
(StageDetails)this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].DataBoundItem;
int expressionIcode = stageDetail.ExpressionIcode;
MessageBox.Show("Expression ID = " +
expressionIcode.ToString());
}

if (IsANonHeaderLinkCell(e))
{
StageDetails stageDetail =
(StageDetails)this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].DataBoundItem;

Form frm = new ExpressionTiff();
frm.ShowDialog();

}

}


private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs
cellEvent)
{
if (this.dataGridView1.Columns[cellEvent.ColumnIndex] is
DataGridViewButtonColumn &&
cellEvent.RowIndex != -1)
{ return true; }
else { return (false); }
}

private bool IsANonHeaderLinkCell(DataGridViewCellEventArgs cellEvent)
{
if (this.dataGridView1.Columns[cellEvent.ColumnIndex] is
DataGridViewLinkColumn && cellEvent.RowIndex != -1)
{
return true;
}
else
{
return false;
}
}

//void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
//{
// // throw new Exception("The method or operation is not
implemented.");
//}



//////////////////////////
private Rectangle dragBoxFromMouseDown;

private int rowIndexFromMouseDown;

private int rowIndexOfItemUnderMouseToDrop;

private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{

if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{

// If the mouse moves outside the rectangle, start the drag.

if (dragBoxFromMouseDown != Rectangle.Empty &&

!dragBoxFromMouseDown.Contains(e.X, e.Y))
{



// Proceed with the drag and drop, passing in the list
item.

DragDropEffects dropEffect = dataGridView1.DoDragDrop(
dataGridView1.Rows[rowIndexFromMouseDown],
DragDropEffects.Move);

}

}

}



private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{

// Get the index of the item the mouse is below.

rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;



if (rowIndexFromMouseDown != -1)
{

// Remember the point where the mouse down occurred.
// The DragSize indicates the size that the mouse can move
// before a drag event should be started.

Size dragSize = SystemInformation.DragSize;



// Create a rectangle using the DragSize, with the mouse
position being

// at the center of the rectangle.

dragBoxFromMouseDown = new Rectangle(new Point(e.X -
(dragSize.Width / 2),

e.Y - (dragSize.Height / 2)),
dragSize);

}

else

// Reset the rectangle if the mouse is not over an item in
the ListBox.

dragBoxFromMouseDown = Rectangle.Empty;

}



private void dataGridView1_DragOver(object sender, DragEventArgs e)
{

e.Effect = DragDropEffects.Move;

}



private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{

// The mouse locations are relative to the screen, so they must
be

// converted to client coordinates.

Point clientPoint = dataGridView1.PointToClient(new Point(e.X,
e.Y));



// Get the row index of the item the mouse is below.

rowIndexOfItemUnderMouseToDrop =

dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;



// If the drag operation was a move then remove and insert the
row.

if (e.Effect == DragDropEffects.Move)
{

DataGridViewRow rowToMove = e.Data.GetData(
typeof(DataGridViewRow)) as DataGridViewRow;

dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);

dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop,
rowToMove);



}

}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}



}
}

"Zhi-Xin Ye [MSFT]" wrote:
Quote:

> Hi,
>
> Thank you for using Microsoft Managed Newsgroup Service, it's my pleasure
> to work with you on this issue.
>
> As I understand, you have a DataGridView control which is data bound, one
> of its columns is a DataGridViewLinkColumn. And you handle the
> CellDoubleClick event, in the event handler, you add content for the
> current row, and you found if the content in the DataGridViewLinkCell is
> long enough to be in the place where the user double clicks, the
> CellContentClick event fires as well.
>
> I try to reproduce this problem with the following code, when I double
> click on the DataGridViewLinkCell, the CellDoubleClick event fires and
> content is added to the cell, but the CellContentClick event does not fire.
>
> private void Form1_Load(object sender, EventArgs e)
> {
> DataTable dt = new DataTable();
> dt.Columns.Add("c1", typeof(int));
> dt.Columns.Add("c2");
> for (int j = 0; j < 50; j++)
> {
> dt.Rows.Add(j, "");
> }
>
> this.dataGridView1.DataSource = dt;
>
> DataGridViewLinkColumn linkCol = new DataGridViewLinkColumn();
> linkCol.DataPropertyName = "c2";
> this.dataGridView1.Columns.Add(linkCol);
>
> this.dataGridView1.CellDoubleClick += new
> DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
> this.dataGridView1.CellContentClick += new
> DataGridViewCellEventHandler(dataGridView1_CellContentClick);
> }
>
> void dataGridView1_CellContentClick(object sender,
> DataGridViewCellEventArgs e)
> {
> Console.WriteLine("Cell Content");
> }
>
> void dataGridView1_CellDoubleClick(object sender,
> DataGridViewCellEventArgs e)
> {
> Console.WriteLine("Cell double");
>
> if (e.ColumnIndex == 2)
> {
> DataRowView drv =
> this.dataGridView1.CurrentRow.DataBoundItem as DataRowView;
> drv["c2"] =
> "asccccccccccccccccccccccccccccccccccccccccccccc";
> }
> }
>
> If I misunderstand you, please let me know. I look forward to your reply.
>
> 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:
> msdnmg@xxxxxx.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subs...#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/subs.../aa948874.aspx
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
My System SpecsSystem Spec
Old 02-05-2009   #4 (permalink)
Zhi-Xin Ye [MSFT]


 
 

RE: dataGridView CellContentClick fires after CellDoubleClick

Hi,

I have received your sample code via email. I notice that you use
Form.ShowDialog() method to display the form, this method create a modal
dialog, a modal dialog runs its own message loop and would disrupt the
application's main message loop. To avoid this behavior, you can call the
Form.Show() method instead to display a modeless dialog.

However, If the main form should be disabled when displaying the new form,
you can use a modeless dialog box to simulate a modal dialog box. To do
this, disable the application's main form first, then calling the
Form.Show() method to display the new form, and enabling the form when the
new form closed. This approach causes the normal message processing
sequence to be followed, so the CellContentClick event won't fire in this
scenario. For example:

void dataGridView1_CellDoubleClick(object sender,
DataGridViewCellEventArgs e)
{
Console.WriteLine("Cell double");

this.Enabled = false;
Form f = new Form();
f.FormClosed += new FormClosedEventHandler(f_FormClosed);
f.Show();

if (e.ColumnIndex == 2)
{
DataRowView drv =
this.dataGridView1.CurrentRow.DataBoundItem as DataRowView;
drv["c2"] =
"cccccccccccccccccccccccccccccccccccccccccccccccc";
}

}

void f_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
}


Please try my suggestion and let me know whether it makes sense to you. If
you have any questions or concerns, please do not hesitate to feed back.

Have a nice 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:
msdnmg@xxxxxx.

This posting is provided "AS IS" with no warranties, and confers no rights.

My System SpecsSystem Spec
Old 02-05-2009   #5 (permalink)
QSIDeveloper


 
 

RE: dataGridView CellContentClick fires after CellDoubleClick

Thank you for you response
I tried your workaround, while this is OK in a simple example in a real
application I need to get the DialogResult value back from the form. I can’t
do that with this workaround. I need to show the form as a modal dialog.
Why is the CellContentClick event firing if there is no content in the cell
when the user clicks it? Is this a bug? I need a solution that works with a
modal dialog.


"Zhi-Xin Ye [MSFT]" wrote:
Quote:

> Hi,
>
> I have received your sample code via email. I notice that you use
> Form.ShowDialog() method to display the form, this method create a modal
> dialog, a modal dialog runs its own message loop and would disrupt the
> application's main message loop. To avoid this behavior, you can call the
> Form.Show() method instead to display a modeless dialog.
>
> However, If the main form should be disabled when displaying the new form,
> you can use a modeless dialog box to simulate a modal dialog box. To do
> this, disable the application's main form first, then calling the
> Form.Show() method to display the new form, and enabling the form when the
> new form closed. This approach causes the normal message processing
> sequence to be followed, so the CellContentClick event won't fire in this
> scenario. For example:
>
> void dataGridView1_CellDoubleClick(object sender,
> DataGridViewCellEventArgs e)
> {
> Console.WriteLine("Cell double");
>
> this.Enabled = false;
> Form f = new Form();
> f.FormClosed += new FormClosedEventHandler(f_FormClosed);
> f.Show();
>
> if (e.ColumnIndex == 2)
> {
> DataRowView drv =
> this.dataGridView1.CurrentRow.DataBoundItem as DataRowView;
> drv["c2"] =
> "cccccccccccccccccccccccccccccccccccccccccccccccc";
> }
>
> }
>
> void f_FormClosed(object sender, FormClosedEventArgs e)
> {
> this.Enabled = true;
> }
>
>
> Please try my suggestion and let me know whether it makes sense to you. If
> you have any questions or concerns, please do not hesitate to feed back.
>
> Have a nice 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:
> msdnmg@xxxxxx.
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
My System SpecsSystem Spec
Old 02-10-2009   #6 (permalink)
Zhi-Xin Ye [MSFT]


 
 

RE: dataGridView CellContentClick fires after CellDoubleClick

Hi,

I'm glad to hear that my workaround worked!

I've checked this problem with our developers, they said that it's not
recommend to open a dialog especially a modal dialog in the CellDoubleClick
event as it would mess up the message chain, this is currently by design.
However, for the time being, if you find it's necessary to open a modal
dialog in CellDoubleClick event, you can use my workaround.

If you have any questions or concerns, please do not hesitate to let me
know, I will be happy of assistance. Have a splendid 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:
msdnmg@xxxxxx.

This posting is provided "AS IS" with no warranties, and confers no rights.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Serious Fires Chillout Room
leave event fires disabling textbox below -cursor stays in 1st tex .NET General
Windows Defender: Event 3004 never fires Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46