Search code examples
c#winformsdatagridviewcell

How to identify the specific cell hovered over in DataGridView


I have put a picture at the end of all datagridview rows to delete row when pressed. enter image description here I want to change color of that picture on specific cell mouseover (Inorder to indicate it is an interactive button to the user).

However in all solutions I found full DGV mouseover is explianed. What I need: Learn how to find the specific cell hovered over during cell mouseover.


Solution

  • If this is WindowsForms:

    //when mouse is over cell
        private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Black;
            }
        }
    //when mouse is leaving cell
        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                dataGridView1[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
            }
        }