Code Snippet
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
this.dataGridView1.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);
int columnIndex = -2;
int rowIndex = -2;
void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
DataGridView.HitTestInfo info = this.dataGridView1.HitTest(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
columnIndex = info.ColumnIndex;
rowIndex = info.RowIndex;
this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex);
if (info.ColumnIndex - 1 >= -1)
this.dataGridView1.InvalidateCell(info.ColumnIndex - 1, info.RowIndex);
if (info.ColumnIndex + 1 <= this.dataGridView1.ColumnCount - 1)
this.dataGridView1.InvalidateCell(info.ColumnIndex + 1, info.RowIndex);
if (info.RowIndex - 1 >= -1)
this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex - 1);
if (info.RowIndex + 1 < this.dataGridView1.NewRowIndex)
this.dataGridView1.InvalidateCell(info.ColumnIndex, info.RowIndex + 1);
}
else
{
columnIndex = -2;
rowIndex = -2;
}
}
void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == columnIndex && e.RowIndex == rowIndex)
{
using (
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(Pens.Blue, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the text content of the cell, ignoring alignment.
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}