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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With