none
any way to show DataGridView gridline like listview?

    Question

  • I mean, datagridview only showgridline for cells with data, is it possible to make it show gridline for all viewable areas, like listview?

    thanks in advance.
    Tuesday, October 02, 2007 3:28 PM

Answers

  • You have to override the OnPaint event to draw GridLines in the no data area, as my sample shows

    Code Block

       
        class
    GridLineDataGridView : DataGridView

        {

            public GridLineDataGridView()

            {

                this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

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

     

                int w = this.RowHeadersWidth - 1;

                for (int j = 0; j < this.ColumnCount; j++)

                {

                    g.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++)

                {

                    e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight);

                }

            }

        }

     

     

    Monday, October 08, 2007 12:20 PM

All replies

  • You have to override the OnPaint event to draw GridLines in the no data area, as my sample shows

    Code Block

       
        class
    GridLineDataGridView : DataGridView

        {

            public GridLineDataGridView()

            {

                this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

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

     

                int w = this.RowHeadersWidth - 1;

                for (int j = 0; j < this.ColumnCount; j++)

                {

                    g.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++)

                {

                    e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight);

                }

            }

        }

     

     

    Monday, October 08, 2007 12:20 PM
  • many thanks.
    Monday, October 08, 2007 1:35 PM
  • Can i get vb code of the above

    Friday, September 04, 2009 7:14 AM
  • Thanks a lot, It is working fine but I have requirement like DataGridViewAutoSizeColumnsMode .None , so while Hscroll this is not working well. I try to refresh but not working.Do you have any suggestion?

    Aum Universe
    Friday, January 28, 2011 11:37 AM