码迷,mamicode.com
首页 > Windows程序 > 详细

如何让 dataGridView 的空白区域自动用网格填充

时间:2021-05-24 14:58:56      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:bitmap   htm   idt   cte   code   nta   空白   ++   pre   

DataGridView 空数据像Excel空网格一样(DataGridView Excel like empty grid)

 

class GridLineDataGridView : DataGridView
    {
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            int rowHeight = this.RowTemplate.Height;
 
            int h = this.ColumnHeadersHeight + rowHeight * this.RowCount ;
            int imgWidth = this.Width - 2;
            Rectangle rFrame = new Rectangle(0, 0, imgWidth, rowHeight);
            Rectangle rFill = new Rectangle(1, 1, imgWidth - 2, rowHeight);
            Rectangle rowHeader = new Rectangle(2, 2, this.RowHeadersWidth - 3, rowHeight);
 
            Pen pen = new Pen(this.GridColor, 1);
 
            Bitmap rowImg = new Bitmap(imgWidth, rowHeight);
            Graphics g = Graphics.FromImage(rowImg);
            g.DrawRectangle(pen, rFrame);
            g.FillRectangle(new SolidBrush(this.DefaultCellStyle.BackColor), rFill);
            g.FillRectangle(new SolidBrush(this.RowHeadersDefaultCellStyle.BackColor), rowHeader);
 
            Bitmap rowImgAAlternative = rowImg.Clone() as Bitmap;
            Graphics g2 = Graphics.FromImage(rowImgAAlternative);
            rFill.X += this.RowHeadersWidth - 1;
            g2.FillRectangle(new SolidBrush(this.AlternatingRowsDefaultCellStyle.BackColor), rFill);
 
            int w = this.RowHeadersWidth - 1;
            for (int j = 0; j < this.ColumnCount; j++)
            {
                g.DrawLine(pen, new Point(w, 0), new Point(w, rowHeight));
                g2.DrawLine(pen, new Point(w, 0), new Point(w, rowHeight));
                w += this.Columns[j].Width;
            }
 
            int loop = (this.Height - h) / rowHeight;
            for (int j = 0; j < loop + 1; j++)
            {
                int index = this.RowCount + j;
                if (index % 2 == 0)
                {
                    e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight);
                }
                else
                {
                    e.Graphics.DrawImage(rowImgAAlternative, 1, h + j * rowHeight);
                }
            }
        }
    }

 

 

 

转 : https://www.it1352.com/1336145.html

 

如何让 dataGridView 的空白区域自动用网格填充

标签:bitmap   htm   idt   cte   code   nta   空白   ++   pre   

原文地址:https://www.cnblogs.com/fps2tao/p/14781449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!