views:

93

answers:

1

Hi,

Does anybody know how can I determine where the user clicked in a DataGrid control ? I'm using .NET CF with Windows Mobile 6. What I need to know is whether the user clicked on the selected cell or on an empty area (not covered by columns or rows). Is there a way to retrieve it from EventArgs ?

Thanks.

+1  A: 

This is from MSDN

private void myDataGrid_MouseDown(object sender, 
System.Windows.Forms.MouseEventArgs e)
{
   DataGrid myGrid = (DataGrid) sender;
   System.Windows.Forms.DataGrid.HitTestInfo hti;
   hti = myGrid.HitTest(e.X, e.Y);
   string message = "You clicked ";

   switch (hti.Type) 
   {
      case System.Windows.Forms.DataGrid.HitTestType.None :
         message += "the background.";
         break;
      case System.Windows.Forms.DataGrid.HitTestType.Cell :
         message += "cell at row " + hti.Row + ", col " + hti.Column;
         break;
      case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader :
         message += "the column header for column " + hti.Column;
         break;
      case System.Windows.Forms.DataGrid.HitTestType.RowHeader :
         message += "the row header for row " + hti.Row;
         break;
      case System.Windows.Forms.DataGrid.HitTestType.ColumnResize :
         message += "the column resizer for column " + hti.Column;
         break;
      case System.Windows.Forms.DataGrid.HitTestType.RowResize :
         message += "the row resizer for row " + hti.Row;
         break;
      case System.Windows.Forms.DataGrid.HitTestType.Caption :
         message += "the caption";
         break;
      case System.Windows.Forms.DataGrid.HitTestType.ParentRows :
         message += "the parent row";
         break;
      }

      Console.WriteLine(message);
}
Oskar Kjellin
Great! Thank You!
user1112111