Seleccionar toda una fila con un clic en una celda

2050 vistas

La clase System.Windows.Forms.DataGrid.HitTestInfo permite detectar qué parte de un DataGrid se ha seleccionado con un clic. Para hacerlo, tendremos uqe usar el método HitTest del Datagrid:



csharp
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. //...
  5. private void DG_MouseUp(object sender, MouseEventArgs e)
  6. {
  7.   // recuperamos las coordenadas del ratón
  8.   Point p = new Point(e.X, e.Y);
  9.   // determinamos la parte clicada del DataGrid
  10.   DataGrid.HitTestInfo HTI = DG.HitTest(p);
  11.   // si es de tipo 'celda'
  12.   if(HTI.Type == DataGrid.HitTestType.Cell)
  13.     {
  14.         // definimos la celda en curso
  15.         DG.CurrentCell = new DataGridCell(HTI.Row, HTI.Column);
  16.         // seleccionamos la línea correspondiente
  17.         DG.Select(HTI.Row);
  18.     }
  19. }