明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

C# dataGridView控件某单元格,间隔地变换背景色

Posted on 2008-11-20 10:17  且行且思  阅读(2854)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace PingBoTest
{
    
public partial class dtViewColor : Form
    {
        DataTable dtView 
= new DataTable();
        Thread cmpThread;
        
public dtViewColor()
        {
            InitializeComponent();
        }

        
private void dtViewColor_Load(object sender, EventArgs e)        {
            DataColumn dataColumn0 
= new DataColumn("PlayID"typeof(string));
            dtView.Columns.Add(dataColumn0);
            DataColumn dataColumn1 
= new DataColumn("TourName"typeof(string));
            dtView.Columns.Add(dataColumn1);
            DataColumn dataColumn2 
= new DataColumn("StartTime"typeof(string));
            dtView.Columns.Add(dataColumn2);
            DataColumn dataColumn3 
= new DataColumn("HostTeam"typeof(string));
            dtView.Columns.Add(dataColumn3);
            DataColumn dataColumn4 
= new DataColumn("GuestTeam"typeof(string));
            dtView.Columns.Add(dataColumn4);
            DataColumn dataColumn5 
= new DataColumn("GiveNumber"typeof(float));
            dtView.Columns.Add(dataColumn5);
            DataColumn dataColumn6 
= new DataColumn("HostRate"typeof(float));
            dtView.Columns.Add(dataColumn6);
            DataColumn dataColumn7 
= new DataColumn("GuestRate"typeof(float));
            dtView.Columns.Add(dataColumn7);   
            
for (int y = 0; y < 10; y++)
            {
                
object[] insertData = new object[7];
                insertData[
0= y;
                insertData[
1= y + 1;
                insertData[
2= y + 1;
                insertData[
3= y + 1;
                insertData[
4= y + 1;
                insertData[
5= y + 1;
                insertData[
6= y + 1;

                dtView.Rows.Add(insertData);
                dataGridView1.DataSource 
= dtView;
                dataGridView1.AutoSizeColumnsMode 
= DataGridViewAutoSizeColumnsMode.Fill;
            }
        }


        
private void button2_Click(object sender, EventArgs e)
        {
            
this.label1.Text = "";
            
this.label2.Text = "";
            
if (cmpThread != null && cmpThread.IsAlive)
            {                
                cmpThread.Abort();
            }

            cmpThread 
= new Thread(new ThreadStart(GridColor));
            cmpThread.IsBackground 
= true
            cmpThread.Start();
           
        }

        
private void GridColor()
        {
            
int time1 = Environment.TickCount;


            
for (int i = 0; i < this.numericUpDown1.Value; i++)   
            {

                dataGridView1.Rows[
2].Cells[2].Style.BackColor = Color.CadetBlue;
                dataGridView1.Rows[
4].Cells[5].Style.BackColor = Color.DarkGray;
                Thread.Sleep(
800);

                dataGridView1.Rows[
2].Cells[2].Style.BackColor = Color.White;
                dataGridView1.Rows[
4].Cells[5].Style.BackColor = Color.White;
                
int time2 = Environment.TickCount - time1;
                label1.Invoke(
new ChangeString(SetrichTextBox1Text), time2.ToString(), i.ToString());
            }
        }

        
private delegate void ChangeString(string Txt , string time);//这个代理能异步调用设置文本框(可以多参数)
        private void SetrichTextBox1Text(string Txt, string time)//此部分实际上是主线程调用的(可以多参数)
        {
           
this.label1.Text = Txt ;
           
this.label2.Text = time; 
        }
        

       
    }
}