.NET 2.0: Right click on DataGridView and Cell Selection

  • Thread starter Thread starter A.M-SG
  • Start date Start date
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
 
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.
 
Back
Top