Reputation: 456
I have put a picture at the end of all datagridview rows to delete row when pressed.
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.
Upvotes: 2
Views: 5378
Reputation: 8556
Old question, and @titol's answer is good, but I don't like to use the event CellMouseMove
for this (it triggers too often).
I suggest capturing when the mouse enter with CellMouseEnter
and, of course, CellMouseLeave
. Like this:
void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e) {
if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
var gv = sender as DataGridView;
var column = gv.Columns[e.ColumnIndex];
if (myLogicToCheckIfIsTrashButtonColumn(column)) {
gv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.Black;
gv.InvalidateCell(gv.Rows[e.RowIndex].Cells[e.ColumnIndex]);
}
}
void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) {
if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
var gv = sender as DataGridView;
var column = gv.Columns[e.ColumnIndex];
if (myLogicToCheckIfIsTrashButtonColumn(column)) {
gv[e.ColumnIndex, e.RowIndex].Style.BackColor = Color.White;
gv.InvalidateCell(gv.Rows[e.RowIndex].Cells[e.ColumnIndex]);
}
}
Upvotes: 2
Reputation: 1149
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;
}
}
Upvotes: 5