Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel
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 ?
Posted 14-Nov-12 5:12am
TGIO360
Comments
digimanus at 14-Nov-12 11:26am
   
create a datatable with the desired number of rows and cells and bind that to the GridView
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
                }
            }
        }
    }
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100