C# - Coordinates to Col/Row index

When using a DataGrid or a DataGridView control, have you ever had mouse events such as MouseDown or MouseClick, where you wanted to know in which cell (or part of the table) is the pointer pointing to? You have the X and Y coordinates passed in the MouseEventArgs parameter, but how would you translate them to column and row indices?

One way is to apply a long formula involving those coordinates, the size of the table, sizes of cells, headers, etc... But obviously, that's tedious for a C# developer. That's the primitive way (or the C way) of handling it.

Thanks to a workmate, I discovered the magic of the HitTestInfo class. An instance of this class will give you the row and column indexes amongst other information. To create the object, say in a MouseDown event, you do the following:

private void dataGridView1_MouseDown( object sender, MouseEventArgs e )
{
System.Windows.Forms.DataGridView.HitTestInfo h = dataGridView1.HitTest( e.X, e.Y );
}

A DataGridView object contains the method HitTest(), which, given the x and y coordinates, returns an object of type HitTestInfo with all the information you need. Note the namespace of the class: System.Windows.Forms.DataGridView. If you are using a DataGrid, the class would be System.Windows.Forms.DataGrid.HitTestInfo (2 different classes).

With our object created, we can get various useful information (much more useful than x and y).

HitTestInfo.Type gets what part of the table is at the coordinates (Ex. column header, cell, etc...)
HitTestInfo.ColumnIndex & HitTestInfo.RowIndex are self explanatory.

if ( h.Type == System.Windows.Forms.DataGridViewHitTestType.ColumnHeader )
MessageBox.Show( "You clicked on a column header." );
else
MessageBox.Show( h.Type.ToString() + '(' + h.ColumnIndex + ',' + h.RowIndex + ')' );

Note that if you are using a DataGrid, the HitTestInfo.Type returns an element from the enumeration System.Windows.Forms.DataGrid.HitTestType.

Bike Road Trip

Riding forth, from south to north

These are gonna be my longest rides so far:

Friday - San Gwann to Hal Far (and back)
~ 13.86 km (x2)
(Done in 1 hour 5 minutes)


Saturday - San Gwann to Cirkewwa
~ 22.85 km
(Done in ~5 hours due to wrong path taken and tire problems)


Saturday/Sunday - Around in Gozo

Sunday - Cirkewwa to San Gwann
~ 22.85 km

Tags: , | 1 comments |

Hello World!

Greetings all.

So I finally decided to get a blog running. This is what boredom at work sometimes leads to. If you are at work but not working at all, that does not mean you are wasting your time. That time may be being used in creativity (such as writing blogs), so one shouldn't feel guilty of "wasting" time at work.

But anyhow, as the first blog, I'll say that despite being a Maltese guy, I'll keep all this in English, for the potential non-Maltese visitors.

After (I admit) a few hours of looking up blogging hosts, themes, etc... I think it's time to get back to work. So have a good day all (and good night to the potential visitors from the other side of the world).