DataGridView Excel像空网格一样 [英] DataGridView Excel like empty grid

查看:122
本文介绍了DataGridView Excel像空网格一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像在excel中一样更改datagridview,如果没有数据,无论如何它必须显示空网格.但是不幸的是,我还没有为此找到合适的解决方案.您可以给我提示如何自己完成操作或在哪里准备解决方案吗?

Hi i need to change datagridview like in excel, if there is no data it must show empty grid anyways. But unfortunately i cant find ready solution yet for this. Can u give me tips how to do it myself or where to get ready solution ?

推荐答案

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/2ecb07b1-f3c1-40b9 -8c82-477c0948fa7c/ [ ^ ]


好吧,我找到了解决方案,它很简单,只是添加了自定义控件:

Well i found solution, its simple, just added custom control :

Code Snippet
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);
                }
            }
        }
    }


这篇关于DataGridView Excel像空网格一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆