Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 594
Mika Jones Avatar asked Dec 19 '22 03:12

Mika Jones


2 Answers

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.

like image 60
Joel Priddy Avatar answered Dec 31 '22 13:12

Joel Priddy


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;
            }
        }
like image 40
MOHAMED EL JIHAOUI Avatar answered Dec 31 '22 13:12

MOHAMED EL JIHAOUI