4
Answers

Changing a Cursor and DataGridviewCell events.

Photo of Dave

Dave

15y
13.4k
1
Hi,
I am having trouble controlling my cursor behaviour on a forms app. I refer to this pic of part of a DataGridView.
My aim - for the cursor to change to a hand when it is over the trash or edit pics.

I have handled the following events:
         private void dgvBookLibrary_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
switch (dgvBookLibrary.Columns[e.ColumnIndex].Name)
{
case "ColumnDelete":
case "ColumnAmend": this.Cursor = Cursors.Hand; break;
default: break;
}
}

private void dgvBookLibrary_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
switch (dgvBookLibrary.Columns[e.ColumnIndex].Name)
{
case "ColumnDelete":
case "ColumnAmend": this.Cursor = Cursors.Arrow; break;
default: break;
}
}

They work so long as I do not roll over one of the hyperlinks in the grid. As soon as I roll over a hyperlink, the cursor remains an arrow, even when I roll over the two pics.

Any ideas?

Answers (4)

1
Photo of Nilanka Dharmadasa
NA 3k 0 15y
Hi Dave,


As kritan said, you do not need mouse leave event. But still there should be another change.


Change the cursor property of datagridview without changing the cursor of the form.


        private void dgvBookLibrary_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.ColumnIndex == 3) || (e.ColumnIndex == 4))
            {
                this.dgvBookLibrary.Cursor = Cursors.Hand;
            }
            else
            {
                this.dgvBookLibrary.Cursor = Cursors.Default;
            }
        }



If you find this answer useful, please do not forget to mark this accepted.
Accepted
0
Photo of Dave
NA 161 0 15y
I take that back, I don't need to handle the CellMouseLeave event. This is my final code (I have a thing for switch statements)

        private void dgvBookLibrary_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1)
            {
                switch (dgvBookLibrary.Columns[e.ColumnIndex].Name)
                {
                    case "ColumnDelete":
                    case "ColumnLendBook":
                    case "ColumnReturn":
                    case "ColumnAmend": dgvBookLibrary.Cursor = Cursors.Hand; break;
                    default: dgvBookLibrary.Cursor = Cursors.Default; break;
                }
            }
        }

0
Photo of Dave
NA 161 0 15y
Thanks guys.
I found that you do need to handle the CellMouseLeave event, otherwise the cursor remains a hand.

But Niki nailed it by pointing out that I should have been chnaging the DataGridView cursor, rather than the Form cursor. I did not think of that at all.

Works fine now.

Cheers!
0
Photo of Kirtan Patel
NA 21.9k 4.1m 15y
Friend,

you need to Write code like Below

if my answer helps you then check "Do you like this Answer check box" please :)

 private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)

        {

           

            //Here i have Assumed that Column Index For Delete is 1 you need to use Your Column Index

            if(e.ColumnIndex == 1)

            {

                this.Cursor = Cursors.Hand;

            }

            else 

            {

                this.Cursor = Cursors.Default;

            }


        }




Next Recommended Forum