Click here to Skip to main content
16,018,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to change a DataGridView BackColor with an interval.I need it shows black and after 5 seconds turns to yellow.I use the following code but it didn't work.
C#
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Black;
Thread.Sleep(5000);
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Yellow;

It just shows the yellow color and black is not shown.Is there anyway to handle this?
Thanks.
Posted
Comments
[no name] 23-Apr-13 13:03pm    
Think about what your code is doing. You set the color to black and then put the thread to sleep so the color cannot update then you set the color to yellow. Use a timer.
mehdi_13 23-Apr-13 14:40pm    
I use the following code but it didn't work.

DateTime start = DateTime.Now;
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Black;
TimeSpan interval = DateTime.Now - start;
while (interval.TotalSeconds<5)
{
interval = DateTime.Now - start;
}
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Yellow;
[no name] 23-Apr-13 15:08pm    
It did not work because that is no how to use a timer.
mehdi_13 23-Apr-13 15:14pm    
could you tell me how to use it?
[no name] 23-Apr-13 15:29pm    
Can you not read the documentation?

1 solution

I handle it with following code but I'm not sure it's the best way or not.
C#
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Black;
Application.DoEvents();
Thread.Sleep(5000);
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Yellow;
 
Share this answer
 
Comments
[no name] 23-Apr-13 15:07pm    
DoEvents is evil
Kenneth Haugland 23-Apr-13 15:11pm    
Thread.Sleep is eviler :-), should use dispatchertimer or a stopwatch instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900