.NET 2.0: Right click on DataGridView and Cell Selection

A

A.M-SG

Hi,



I am using Visual Studio.NET 2005 and .NET 2.0.



I need to configure a DataGridView so it selects the cell when user right
clicks on the cell. What would be the best way to do that?



Any help would be appreciated,

Alan
 
J

Jeffrey Tan[MSFT]

Hi Mark,

Thanks for your post.

You can use DataGridView.HitTest method to get the row and column index of
the cell user right clicked. Then you can set this cell's Selected property
to true. Sample code like this:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo hti=this.dataGridView1.HitTest(e.X, e.Y);
if (hti.Type == DataGridViewHitTestType.Cell)
{
this.dataGridView1.ClearSelection();

this.dataGridView1.Rows[hti.RowIndex].Cells[hti.ColumnIndex].Selected =
true;
}
}
Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top