You can do the blinking by drawing the background yourself on a timer,
and this way avoid having to interact with the styles.
Here is a minimal sample that starts a cell blinking when you click
the button and stops when you reclick the button. To use the code,
just drag a DataGridView and Button onto a form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
#region Get the DataSource
DataTable dt = new DataTable("MyTable");
int nCols = 4;
int nRows = 10;
for (int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}",
i)));
for (int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < nCols; j++)
dr[j] = string.Format("row{0} col{1}", i, j);
dt.Rows.Add(dr);
}
#endregion
dataGridView1.DataSource = dt;
dataGridView1.CellPainting += new
DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
}
void dataGridView1_CellPainting(object sender,
DataGridViewCellPaintingEventArgs e)
{
if (offOn && blinkers.ContainsKey(new Point(e.ColumnIndex,
e.RowIndex)))
{
using (Brush b = new SolidBrush(blinkers[new
Point(e.ColumnIndex, e.RowIndex)]))
{
e.Graphics.FillRectangle(b, e.CellBounds);
e.Paint(e.ClipBounds, DataGridViewPaintParts.All &
~DataGridViewPaintParts.Background);
e.Handled = true;
}
}
}
Dictionary<Point, Color> blinkers = new Dictionary<Point,
Color>();
bool offOn = true;
Timer t;
private void button1_Click(object sender, EventArgs e)
{
if (blinkers.Count == 0)
{
blinkers.Add(new Point(1, 2), Color.Red);
t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 200;
offOn = true;
t.Start();
}
else
{
offOn = false;
foreach (Point pt in blinkers.Keys)
{
dataGridView1.InvalidateCell(pt.X, pt.Y);
}
blinkers.Clear();
t.Stop();
t.Dispose();
t = null;
}
}
void t_Tick(object sender, EventArgs e)
{
foreach (Point pt in blinkers.Keys)
{
dataGridView1.InvalidateCell(pt.X, pt.Y);
}
offOn = !offOn;
}
}
}
================
Clay Burch
Syncfusion, Inc.
Lee Gillie - 30 Aug 2007 20:08 GMT
Thanks Clay, your example is golden.
All my best - Lee Gillie
> You can do the blinking by drawing the background yourself on a timer,
> and this way avoid having to interact with the styles.