Mika Jones
Mika Jones

Reputation: 307

Use hand cursor only on a certain column of DataGridView

I am writing a form application in C#, and I have a DataGridView, which displays data from SQL server. These data contain a column called Remove and all rows for this column contain the string Remove. Now I want to make all cells of this column look like a button by changing the background color, and using a hand cursor.

My problem is, I cannot use a hand cursor only on this column. What I want is, when the mouse is over any rows of this Remove column, change the mouse pointer to a hand cursor, but not anywhere else.

for(int i=0; i<myDataGridView.RowCount; i++){
    myDataGridView.Cursor = Cursors.Hand;
}

does not do what I want because the mouse pointer becomes the hand cursor everywhere on the DataGridView, rather than only on the Remove column. I tried something like

for(int i=0; i<myDataGridView.RowCount; i++){
    myDataGridView.Columns["Remove"].Cursor = Cursors.Hand;
}

but this gives an error:

System.Windows.Forms.DataGridViewColumn does not contain a definition for "Cursor".

Is there any good way to achieve this? Thank you.

Upvotes: 6

Views: 7915

Answers (2)

MOHAMED EL JIHAOUI
MOHAMED EL JIHAOUI

Reputation: 101

use this code , it work for me

    private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            string colname = dataGridView1.Columns[e.ColumnIndex].Name;
           if(colname!="btnEdit" && colname!= "btnDelete")
            {
                dataGridView1.Cursor = Cursors.Default;
            }
            else
            {
                dataGridView1.Cursor = Cursors.Hand;
            }
        }

Upvotes: 9

Joel Priddy
Joel Priddy

Reputation: 441

Try tapping into the OnCellMouseEnter event of the DataGridView. Once the event fires, you can determine which column the cell is in and change the cursor as appropriate.

Upvotes: 11

Related Questions