HitTestInfo Returning Wrong Column

  • Thread starter Thread starter jim
  • Start date Start date
J

jim

I've been working with the hittestinfo class in my application trying to
determine what column my user has clicked. Everything works fine so long as
the columns are set to their default 75 pixel width. When I change the width
of the column either in my columnstyle or in the datagrid control at
runtime, the incorrect column is being returned.

here's the code...

Dim tempGrid as DataGrid = DirectCast(sender, DataGrid)

'--- Select the whole row when the user clicks in the cell area ---'
Dim pt as New Point(e.X, e.Y)
Dim ht as DataGrid.HitTestInfo = Me.grdPoItems.HiTest(pt)

If ((ht.Row > -1) AndAlso (ht.Column > -1)) Then
MessageBox.Show(cstr(ht.Column))
End If

I have this placed inside a DataGrid.MouseUp event handler.

I'm working with VS 2003, Framework 1.1, No SP's installed.

Has anyone else seen this behavior?

jim
 
I've never seen that behavior. Have you tried using Cursor.Position?

Point cursorPosition = Cursor.Position;
Point pt = dataGrid1.PointToClient(cursorPosition);
System.Windows.Forms.DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);

Do you have any Hidden (Width = 0) columns?

HTH;
Eric Cadwell
http://www.origincontrols.com
 
Hi Eric, thanks for your help.

I actually tried looking *a little closer* at the msdn documentation for the
hittestinfo class.

the example provided specifically states that the hittest is implemented
from the mousedown event rather than the mouseup event (it doesn't say that
it has to be...). I placed my code in mousedown and the correct column seems
to be returned. The only problem is that what I wanted to do was select the
entire row if the selected column was defined as readonly, but selecting the
entire row seems to get wiped out when called form a mousedown event (I'm
assuming paint is called after mousedown but before mouseup here, hence the
different behavior...). I'm not exactly sure why mousedown vs. mouseup would
make a difference for the hittest but I would be interested in an
explanation from anyone.

Anyways, since my aim was to disallow editing in cells for all but one
column in my grid I've implemented this the following way:

In my mouseup procedure:

(typed not copied)

dim tempGrid as DataGrid = DirectCast(sender, DataGrid)

if ((tempGrid.Name = "grdReceived") AndAlso (Not
tempGrid.CurrentCell.ColumnNumber = 4))

tempGrid.Select(tempGrid.CurrentCell.RowNumber)

End If

thanks again for your response!

Jim
 
Back
Top