Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / August 2007

Tip: Looking for answers? Try searching our database.

DataGridViewCell - how to make a cell blink?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lee Gillie - 29 Aug 2007 19:13 GMT
Given the rather complex style inheritance model that exists for
determining the colors and style and so forth that a cell actually
displays in, I am trying to find a way to highlight the cell, and put it
back, such that when it is all done blinking I can completely restore it
to original appearance. It is not obvious, and not as straight forward
as one might think.

Thus such as:

    ' capture original style of cell
    savestyle = cell.style.clone

        ...

    ' blink the cell
    t = cell.style.forecolor
    cell.style.forecolor = cell.style.backcolor
    cell.style.backcolor = t

        ...

    ' when finished blinking restore orginal style of cell
    cell.style = savestyle.clone

does not work, nor does ApplyStyle, and other things I've tried.

Timers and on-state off-state management are no problem, not the issue,
and not the question.
ClayB - 30 Aug 2007 18:53 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.