Home » VB.NetRSS

C# click mouse event on a datagridview row

I know that this subject has been around for several years but I am having a problem getting it to work. I need to be able to capture a "click mouse event" on a datagridview row. I have tried many of the approches seen here without success. I am running VS 2010 and C# 2010 on Windows XP Pro with SP3. My app has two dataGridView controls, dataGridView1 is for sending out messages and dataGridView2 is for receiving messages. On the dataGridView1 I need to select a row and edit the contents and send out a new message. On dataGridView2 I need to delete and view the incoming messages. I have used several functions designed to catch the event on a MouseClick but it does not seem to work. I may have some dataGridView properties set incorrect or other problems. Thanks for your help.
 

8 Answers Found

 

Answer 1

You could try the Cell Clicked event.

If you could post a snippet of your code and we may be able to help further.

 

 

Answer 2

Hi Mike,

   Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. Based on your description please refer to link below:

     http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx CellClick ecent

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx CellContentClick event

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellmouseclick.aspx CellMouseClick event

  Hope these will help you!  

   If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.

 

Answer 3

DeborahK,

Thank you for your reply. I am sending you a test program that should work  on my system but does not.

 

Add From1, button1, dataGridView1, label1

 

using

 

 

System;

using

 

 

System.Data;

using

 

 

System.Windows.Forms;

namespace

 

 

TestDataButton

{

 

 

publicpartialclassForm1 : Form

{

 

 

public Form1()

{

InitializeComponent();

}

 

 

privatevoid button1_Click(object sender, EventArgs e)

{

dataGridView1.ColumnCount = 3;

dataGridView1.Columns[0].Name =

 

"Product ID";

dataGridView1.Columns[1].Name =

 

"Product Name";

dataGridView1.Columns[2].Name =

 

"Product Price";

 

 

 

 

string[]  row  = newstring[] { "1", "Product 1", "1000" };

dataGridView1.Rows.Add(row);

row =

 

newstring[] { "2", "Product 2", "2000" };

dataGridView1.Rows.Add(row);

row =

 

newstring[] { "3", "Product 3", "3000" };

dataGridView1.Rows.Add(row);

row =

 

newstring[] { "4", "Product 4", "4000" };

dataGridView1.Rows.Add(row);

 

 

 

 

DataGridViewButtonColumn btn = newDataGridViewButtonColumn();

dataGridView1.Columns.Add(btn);

btn.HeaderText =

 

"Click Data";

btn.Text =

 

"Click Here";

btn.Name =

 

"btn";

btn.UseColumnTextForButtonValue =

 

true;

}

 

 

privatevoid dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

label1.Text =

 

"GOT IT!";

 

 

if (e.ColumnIndex == 3)

{

 

 

MessageBox.Show((e.RowIndex+1) + " Row " + (e.ColumnIndex+1) + " Column button clicked ");

}

}

}

}

 

Thanks for your help.

MikeT.

 

Answer 4

Hi Mike,

I've test this code on my system, and it has no any error,

what you want to do with this code...?

it seems you want to do nothing but capture  the Cell Click event......

 

if this code doesn't work  on your system,

maybe you should check the namespace of every .cs file...they should be the same~


????????
 

Answer 5

Albert,

Thanks for your reply, You are correct that it should work  on my machine(see my first email). I got a simple dataGridView test program from the net, compiled it and I get the same condition as with my much more involved app. I have checked everything that I know of and still have no resolution. Thanks for your help.

MikeT.

 

 

Answer 6

Hi MikeT2718,

could you post the URL where you got the dataGridView test program source code...?

let me try to compile it~~~

 

Answer 7

Albert,

I am sending you the test program that I am working with. We are behind a security firewall and I cannot provide a url. The test program requires a Windows Form1, button1, datagridview1 and label1 and the code that I am sending you. I believe that I have some properity set incorrectly but I have tried to find it without success. Thank you for your help.

MikeT2718

---------------------------------------

using

 

 

System;

using

 

 

System.Data;

using

 

 

System.Windows.Forms;

using

 

 

System.Data.SqlClient;

 

namespace

 

 

TestDataButton

{

 

 

publicpartialclassForm1 : Form

{

 

 

public Form1()

{

InitializeComponent();

}

 

 

privatevoid button1_Click(object sender, EventArgs e)

{

dataGridView1.ColumnCount = 3;

dataGridView1.Columns[0].Name =

 

"Product ID";

dataGridView1.Columns[1].Name =

 

"Product Name";

dataGridView1.Columns[2].Name =

 

"Product Price";

 

 

string[]  row  = newstring[] { "1", "Product 1", "1000" };

dataGridView1.Rows.Add(row);

row =

 

newstring[] { "2", "Product 2", "2000" };

dataGridView1.Rows.Add(row);

row =

 

newstring[] { "3", "Product 3", "3000" };

dataGridView1.Rows.Add(row);

row =

 

newstring[] { "4", "Product 4", "4000" };

dataGridView1.Rows.Add(row);

 

 

DataGridViewButtonColumn btn = newDataGridViewButtonColumn();

dataGridView1.Columns.Add(btn);

btn.HeaderText =

 

"Click Data";

btn.Text =

 

"Click Here";

btn.Name =

 

"btn";

btn.UseColumnTextForButtonValue =

 

true;

}

 

 

privatevoid dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

{

label1.Text =

 

"GOT IT!";

 

 

if (e.ColumnIndex == 3)

{

 

 

MessageBox.Show((e.RowIndex+1) + " Row " + (e.ColumnIndex+1) + " Column button clicked ");

}

}

}

}

 

 

Answer 8

All,

Thank you all for your help with this problem. I reinstalled the Visual Studio 2010 and the problem  went away. I can now catch events on both CellClick and MouseClick. Your questions lead me to the final answer.

 

MikeT2718.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter