在VS2010开发工具中将datagridview进行重绘

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

        {

            int left = e.CellBounds.Left;

            int top = e.CellBounds.Top;

            int right = e.CellBounds.Right;

            int bottom = e.CellBounds.Bottom;

            e.Handled = true;

            //代䨲码?为a绘?制?列¢D标À¨º题¬a

            if (e.RowIndex == -1)

            {

                for (int i = 0; i < ((DataGridView)sender).ColumnCount; i++)

                {

                    Brush gridBrush = new SolidBrush(Color.Black);

                    Pen gridLinePen = new Pen(gridBrush);

                    e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.CellBounds); //填¬?充?背À3景¡ã

                    //绘?制?文?字Á?

                    // 绘?制?文?字Á?

                    Brush b1 = new SolidBrush(Color.Red);

                    if (e.Value != null)

                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                                                b1, left + 2,

                                                top + 9, StringFormat.GenericDefault);

 

                    e.Graphics.DrawLine(gridLinePen, right - 1,

                               top - 1, right - 1,

                               bottom - 1);   //绘?制?右®¨°边À?界?

 

                    e.Graphics.DrawLine(gridLinePen, left - 1,

                           bottom - 1, right - 1,

                           bottom - 1);    //绘?制?下?边À?界?

 

                }

            }

 

 

 

            //下?面?代䨲码?为a绘?制?单Ì£¤元a格?

 

 

            if (e.RowIndex % 4 == 0 || e.RowIndex % 4 == 1)

            {

 

 

                Brush gridBrush = new SolidBrush(Color.Black);

                Pen gridLinePen = new Pen(gridBrush);

                if ((e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 10) || (e.ColumnIndex == 0 && e.RowIndex % 4 == 0))

                {

                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.CellBounds); //填¬?充?背À3景¡ã

                }

                else

 

                    e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds); //填¬?充?背À3景¡ã

                //绘?制?文?字Á?

                // 绘?制?文?字Á?

                Brush b1 = new SolidBrush(Color.Black);

                if (e.Value != null)

                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                                            b1, left + 2,

                                            top + 1, StringFormat.GenericDefault);

 

                e.Graphics.DrawLine(gridLinePen, right - 1,

                           top - 1, right - 1,

                           bottom - 1);   //绘?制?右®¨°边À?界?

                if (e.ColumnIndex == 0 || e.ColumnIndex == 5)

                { }

                else

 

                    e.Graphics.DrawLine(gridLinePen, left - 1,

                           bottom - 1, right - 1,

                           bottom - 1);    //绘?制?下?边À?界?

 

            }

            if (e.RowIndex % 4 == 2)

            {

 

 

                Brush gridBrush = new SolidBrush(Color.Black);

                Pen gridLinePen = new Pen(gridBrush);

                if (e.ColumnIndex == 0 || e.ColumnIndex == 1 || e.ColumnIndex == 9 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 10)

                {

                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.CellBounds); //填¬?充?背À3景¡ã

                }

                else

 

                    e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);// 绘?制?背À3景¡ã

                // 绘?制?文?字Á?

                Brush b1 = new SolidBrush(Color.Black);

                if (e.Value != null)

                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                                            b1, left + 2,

                                            top + 1, StringFormat.GenericDefault);

                e.Graphics.DrawLine(gridLinePen, right - 1,

                           top - 1, right - 1,

                           bottom - 1);   //绘?制?右®¨°边À?界?

                if (e.ColumnIndex == 0 || e.ColumnIndex == 5 || e.ColumnIndex == 9 || e.ColumnIndex == 7 || e.ColumnIndex == 10)

                { }

                else

 

                    e.Graphics.DrawLine(gridLinePen, left - 1,

                           bottom - 1, right - 1,

                           bottom - 1);    //绘?制?下?边À?界?

 

            }

            if (e.RowIndex % 4 == 3)

            {

 

                Brush gridBrush = new SolidBrush(Color.Black);

                Pen gridLinePen = new Pen(gridBrush);

                if (e.ColumnIndex == 0 || e.ColumnIndex == 1 || e.ColumnIndex == 9 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7 || e.ColumnIndex == 10)

                {

                    e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), e.CellBounds); //填¬?充?背À3景¡ã

                }

                else

                    e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds); //绘?制?背À3景¡ã

                // 绘?制?文?字Á?

                Brush b1 = new SolidBrush(Color.Black);

                if (e.Value != null)

                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

                                            b1, left + 2,

                                            top + 1, StringFormat.GenericDefault);

                e.Graphics.DrawLine(gridLinePen, right - 1,

                           top - 1, right - 1,

                           bottom - 1);   //绘?制?右®¨°边À?界?

 

                e.Graphics.DrawLine(gridLinePen, left - 1,

                       bottom - 1, right - 1,

                       bottom - 1);    //绘?制?下?边À?界?

 

            }

            e.Handled = true;

 

 

        }

 

转载于:https://www.cnblogs.com/zzz-z123/archive/2012/08/18/2645632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值