Skip to content

DataGridView Tricks #1: Set Hand Cursor for Entire Cell

I remember trying a few different solutions for this a couple of weeks ago, none of which would work enabling me to set the “Hand” cursor when hovering your mouse over a particular DataGridView row’s cell. The problem being, that a DataGridViewCell does not support the Cursor property, and a Hyperlink was not suitable as I was using a custom image cell.

It would also be a valuable trick to know if you ever required the entire row to appear “clickable”, perhaps as it may launch a child form etc.

Anyway, so here is my approach to this problem:

Hook on to the event for CellMouseMove:

this.myDataGridView.CellMouseMove += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.myDataGridView_CellMouseMove);

Then configure the event handler, ensuring you alter the integer tested by ColumnIndex to be representational of the one you wish to have a changed cursor on:

private void myDataGridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    // Can potentially throw an 'IndexOutOfRangeException' if not checked.
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0 &&
        e.ColumnIndex == 0) // OR 'this.myDataGridView.Columns["ColName"].Index'
        this.Cursor = Cursors.Hand;
    else
        this.Cursor = Cursors.Default;
}

You will then find when your mouse hovers on the cell, the cursor will change to a Hand, then when you leave the cell it will be changed back to Default.


INTERESTING BUG IF YOU DON’T USE CELLMOUSEMOVE:

This is also the same bug I encountered a couple of weeks ago I mentioned earlier, I do not know if this is a .NET (dare I say it) Framework bug, or my code, but if you attempt to hook the same event handler with respective if/else attached to the CellMouseEnter and CellMouseLeave events, effectively handling them separately, you will find after a while of using the cursor, it claps out and glitches. I found this was most reproducable after resizing a grid column.  Weird.  I have not yet encountered this bug using CellMouseMove.

VN:F [1.8.3_1051]
Rating: 5.0/5 (2 votes cast)
VN:F [1.8.3_1051]
Rating: +1 (from 1 vote)
DataGridView Tricks #1: Set Hand Cursor for Entire Cell5.052
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

One Comment

  1. Veljko

    The CellMouseEnter and CellMouseLeave are a bit more effective in performance because the event is not so frequently fired as CellMouseMove.
    The glitches you receive is probably because you declared both events and in both placed the ‘else’ branch – this is a bit of a problem because both events fire when mouse is moved from one cell to another. So, the sufficient code should be:
    private void grd_CellMouseEnter(….)
    if (CheckIsClickable(e.RowIndex, e.ColumnIndex))
    {
    this.Cursor = Cursors.Hand;
    }

    private void grd_CellMouseLeave(….)
    if (CheckIsClickable(e.RowIndex, e.ColumnIndex))
    {
    this.Cursor = Cursors.Default;
    }

    I’ve tried this and works just fine…

    (The CheckIsClickable is my method that returns true or false based on the current row and column index – columns that hold clickable cells and rows that have data (i.e. > 0 and < grd.Rows.Count))

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 26-Jan-10 at 10:13 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...