Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I select an entire row when the user clicks in a DataGrid?

Handle the DataGrid's MouseUp event, and within the hander call the DataGrid.Select method.

using System.Drawing;
using System.Windows.Forms;

private DataGrid dataGrid1;

// ...
dataGrid1.MouseUp += new MouseEventHandler( dataGrid1_MouseUp );
// ...

private void dataGrid1_MouseUp( object sender, MouseEventArgs e )
{
  Point point = new Point( e.X, e.Y );
  DataGrid.HitTestInfo hti = dataGrid1.HitTest( point );
  if ( hti.Type == DataGrid.HitTestType.Cell )
  {
    dataGrid1.CurrentCell = new DataGridCell( hti.Row, hti.Column );
    dataGrid1.Select( hti.Row );
  }
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter